How to Page Setup using Excel VBA

 When you are going to create pdf or print your worksheet , you can use vba code. This will increase your efficiency. You can easily setup your page. Here I use Set the PrintCommunication property to False to speed up the execution of code that sets PageSetup properties.  PrintTitleRows = "" means no title on page even multile pages atteached in the same file. If you use PrintTitleRows = "$1:$1" that means vba will setup the first column as Title. and when you use Chr(10)  that means line break, we manually use it by pressing Enter Key. Set the PrintCommunication property to True after setting properties to commit all cached PageSetup commandsUsing of &D&T means insert Date and Time.

The following example suspends communication with the printer while setting PageSetup properties.

    1. Sub Page_Setup()
    2.     Application.PrintCommunication = False
    3.     With ActiveSheet.PageSetup
    4.         .PrintTitleRows = ""
    5.         .PrintTitleColumns = ""
    6.     End With
    7.     Application.PrintCommunication = True
    8.     ActiveSheet.PageSetup.PrintArea = ""
    9.     Application.PrintCommunication = False
    10.     With ActiveSheet.PageSetup
    11.         .LeftHeader = ""
    12.         .CenterHeader = "Report For" & Chr(10) & "Nsutradhar"
    13.         .RightHeader = "&D&T"
    14.         .LeftFooter = ""
    15.         .CenterFooter = ""
    16.         .RightFooter = ""
    17.         .Orientation = xlPortrait
    18.     End With
    19.     Application.PrintCommunication = True    
    20. End Sub

Support and feedback

Have any Question? You can comment here.

No comments:

Post a Comment