Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Automatically run Macro before printing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BLW - 24 May 2008 22:38 GMT
I have written this macro to set page breaks once all info is added to the
spreadsheet.  Is there anyway to have the Macro run automatically when the
user prints or previews?  Or is it better is attach it to a button for them
to click before printing?

Thanks for any help - BLW

Sub PageBreak()

   Range("A1").Select
   Do While ActiveCell.Value <> "End"
   ActiveCell.Offset(20, 0).Select
   value1 = ActiveCell.Value
   If value1 = "ReMax" Then
       ActiveCell.EntireRow.Select
       ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
   Else
       Do While ActiveCell.Value = ""
       ActiveCell.Offset(1, 0).Select
           value1 = ActiveCell.Value
           If value1 = "ReMax" Then
               ActiveCell.EntireRow.Select
               ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
           End If
       Loop
   End If
   Loop
End Sub
Gord Dibben - 24 May 2008 23:46 GMT
Private Sub Workbook_BeforePrint(Cancel As Boolean)

 your code or macroname here

End Sub

Which is better?  That would be your preference but clicking a button to
activate a macro then clicking on the Print Icon is not as easy as clicking on
the Print Icon to achieve the same thing.

Gord Dibben  MS Excel MVP

>I have written this macro to set page breaks once all info is added to the
>spreadsheet.  Is there anyway to have the Macro run automatically when the
[quoted text clipped - 24 lines]
>    Loop
>End Sub
Don Guillett - 25 May 2008 00:02 GMT
Look in the ThisWorkbook module for the "before print" event. You probably
want to restrict to a single sheet.
Also, your macro could stand some pruning. Exactly what are you trying to
do? Something like this maybe that sets a pagebreak before each instance of
remax

Sub setpagebreaks()
With Worksheets(1).Range("a1:a500")
   Set c = .Find("ReMax", LookIn:=xlValues)
   If Not c Is Nothing Then
       firstAddress = c.Address
       Do
'           MsgBox c.Row
ActiveSheet.HPageBreaks.Add Before:=c
           Set c = .FindNext(c)
   Loop While Not c Is Nothing And c.Address <> firstAddress
   End If
End With
End Sub

Signature

Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett1@austin.rr.com

>I have written this macro to set page breaks once all info is added to the
> spreadsheet.  Is there anyway to have the Macro run automatically when the
[quoted text clipped - 26 lines]
>    Loop
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.