+ Reply to Thread
Results 1 to 6 of 6

Use VBA to set worksheet formatting

Hybrid View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Use VBA to set worksheet formatting

    I started this macro from macro recorder but it takes a very long time.
    Is there something to do to speed it up?
    Sub PrintFormat()
    ' Macro recorded 11/29/2010 by 35
    '
        Sheets(1).Activate
        With Sheets(1).PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        Sheets(1).PageSetup.PrintArea = ""
        With Sheets(1).PageSetup
            .RightHeader = "&Pof &N"
            .LeftFooter = ""
            .LeftMargin = Application.InchesToPoints(0.5)
            .RightMargin = Application.InchesToPoints(0.5)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.5)
            .FooterMargin = Application.InchesToPoints(0.5)
            .PrintQuality = 600
            .CenterHorizontally = True
            .Orientation = xlPortrait
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
    
        Sheets(2).Activate
        With Sheets(2).PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        Sheets(2).PageSetup.PrintArea = ""
        With Sheets(2).PageSetup
            .LeftHeader = ""
            .RightHeader = "&Pof &N"
            .LeftFooter = ""
            .LeftMargin = Application.InchesToPoints(0.5)
            .RightMargin = Application.InchesToPoints(0.5)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.5)
            .FooterMargin = Application.InchesToPoints(0.5)
            .PrintQuality = 600
            .CenterHorizontally = True
            .Orientation = xlPortrait
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
    
        Sheets(3).Activate
        With Sheets(3).PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
        End With
        Sheets(3).PageSetup.PrintArea = ""
        With Sheets(3).PageSetup
            .RightHeader = "&Pof &N"
            .LeftMargin = Application.InchesToPoints(0.5)
            .RightMargin = Application.InchesToPoints(0.5)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.5)
            .FooterMargin = Application.InchesToPoints(0.5)
            .PrintComments = xlPrintNoComments
            .PrintQuality = 600
            .CenterHorizontally = True
            .CenterVertically = False
            .Orientation = xlLandscape
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .Zoom = 85
        End With
        
    Sheets(1).Activate
    End Sub
    Any hints, tips or examples are appreciated.
    Last edited by Rick_Stanich; 11-29-2010 at 10:33 AM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Use VBA to set worksheet formatting

    try this way:

    Sub PrintFormat()
    For Each sh In ThisWorkbook.Sheets
        With sh.PageSetup
            .PrintTitleRows = ""
            .PrintTitleColumns = ""
            .PrintArea = ""
            .RightHeader = "&Pof &N"
            .LeftFooter = ""
            .LeftMargin = Application.InchesToPoints(0.5)
            .RightMargin = Application.InchesToPoints(0.5)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .HeaderMargin = Application.InchesToPoints(0.5)
            .FooterMargin = Application.InchesToPoints(0.5)
            .PrintQuality = 600
            .CenterHorizontally = True
            .Orientation = xlPortrait
            .PaperSize = xlPaperLetter
            .FirstPageNumber = xlAutomatic
            .Order = xlDownThenOver
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
    Next
    Sheets(1).Activate: End Sub
    Some parameters are as you would like to have as default value so no need to change - the way to speed it up
    Last edited by watersev; 11-29-2010 at 10:30 AM.

  3. #3
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Re: Use VBA to set worksheet formatting

    Hi

    This has been a pain for some time, however you can speed it up using the Excel 4 macro function.

    Here are a couple of links explaining how to go about it.

    http://www.mcgimpsey.com/excel/udfs/pagesetup.html
    http://www.pcreview.co.uk/forums/thread-947330.php

    Regards

    Jeff

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,993

    Re: Use VBA to set worksheet formatting

    Configuring the Printer settings was always very slow, in my experience ... and you're going through the process three times. It also used to depend on the printer driver, though it's a while since I've tried this so it might have improved

    If all the pages are (roughly) the same, try grouping the sheets and recording the macro as you set the parameters once for all the pages. Having recorded the macro, edit it to remove any default settings

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Use VBA to set worksheet formatting

    PageSetup (before XL2010) is notoriously slow due to the communication between VBA and the printer drivers. You should either try to format the sheets ahead of time (e.g. use a template sheet) or use the old XLM PAGE.SETUP method:
    
      Public Sub PageSetupXL4M(Optional LeftHead As String, Optional CenterHead As String, Optional RightHead As String, _
            Optional LeftFoot As String, Optional CenterFoot As String, Optional RightFoot As String, _
            Optional LeftMarginInches As String, Optional RightMarginInches As String, Optional TopMarginInches As String, _
            Optional BottomMarginInches As String, Optional HeaderMarginInches As String, Optional FooterMarginInches As String, _
            Optional PrintHeadings As String, Optional PrintGridlines As String, Optional PrintComments As String, _
            Optional PrintQuality As String, Optional CenterHorizontally As String, Optional CenterVertically As String, _
            Optional Orientation As String, Optional Draft As String, Optional PaperSize As String, _
            Optional FirstPageNumber As String, Optional Order As String, Optional BlackAndWhite As String, _
            Optional Zoom As String)
         'based on a post by John Green in
         'microsoft.public.excel.programming
         'on 21 January 2001:
         'http://google.com/groups?selm=VA.00000b2f.0028c7e5%40mara9"
         Const c As String = ","
         Dim pgSetup As String
         Dim head As String
         Dim foot As String
         If LeftHead <> "" Then head = "&L" & LeftHead
         If CenterHead <> "" Then head = head & "&C" & CenterHead
         If RightHead <> "" Then head = head & "&R" & RightHead
         If Not head = "" Then head = """" & head & """"
         If LeftFoot <> "" Then foot = "&L" & LeftFoot
         If CenterFoot <> "" Then foot = foot & "&C" & CenterFoot
         If RightFoot <> "" Then foot = foot & "&R" & RightFoot
         If Not foot = "" Then foot = """" & foot & """"
            
         pgSetup = "PAGE.SETUP(" & head & c & foot & c & _
           LeftMarginInches & c & RightMarginInches & c & _
           TopMarginInches & c & BottomMarginInches & c & _
           PrintHeadings & c & PrintGridlines & c & _
           CenterHorizontally & c & CenterVertically & c & _
           Orientation & c & PaperSize & c & Zoom & c & _
           FirstPageNumber & c & Order & c & BlackAndWhite & c & _
           PrintQuality & c & HeaderMarginInches & c & _
           FooterMarginInches & c & PrintComments & c & Draft & ")"
         Application.ExecuteExcel4Macro pgSetup
       End Sub
    Note that you need to activate each sheet when using this.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,192

    Re: Use VBA to set worksheet formatting

    Thanks for the help and tips!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1