MS VBA to modify Excel Worksheet Layouts

Experts,

Please find attached a spreadsheet (.xls) that applies a macro to a selected workbook (.xlsm). The worksheets need the layout of Landscape and fit to one page.

The code below calls a macro to do the formatting and the layout is called next.  

How can the code be modified to format the page layouts?

Sub WorksheetsByCodeName(wb As Workbook)
Dim ws As Worksheet
    For Each ws In wb.Worksheets
        If UCase(ws.CodeName) <> "SHEET10" Then
            ' do calculations for all other sheets
            Call McrRisk(ws)
            McrLayout
        End If
    Next ws
End Sub

Sub McrLayout()
'
' File layout orientation
' Keyboard Shortcut: Ctrl+Shift+L
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 0.5
        .FitToPagesTall = 0.5
        .PaperSize = xlPaperLegal
    End With
    Application.PrintCommunication = True
End Sub
SSReportBuilder.xls
LVL 1
CFMIFinancial Systems AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
try changing this

        .FitToPagesWide = 0.5
        .FitToPagesTall = 0.5

with


        .FitToPagesWide = 1
        .FitToPagesTall = 1
0
CFMIFinancial Systems AnalystAuthor Commented:
Yes, changing the size to 1 enabled one of the worksheets to have the correct layout but the others are portait and not fit to 1,  I was hoping that calling the code within each worksheet would provide the correct layout?
Sub WorksheetsByCodeName(wb As Workbook)
Dim ws As Worksheet
    For Each ws In wb.Worksheets
        If UCase(ws.CodeName) <> "SHEET10" Then
            ' do calculations for all other sheets
            Call McrRisk(ws)
            Call McrLayout
        End If
    Next ws
End Sub
0
byundtMechanical EngineerCommented:
McrLayout is working on the active sheet. You should pass it the worksheet to be used instead. I added error -handling because I was having problems with restoring print communication

Sub WorksheetsByCodeName(wb As Workbook)
Dim ws As Worksheet
    For Each ws In wb.Worksheets
        If UCase(ws.CodeName) <> "SHEET10" Then
            ' do calculations for all other sheets
            Call McrRisk(ws)
            Call McrLayout(ws)
        End If
    Next ws
End Sub

Open in new window


Sub McrLayout(ws As Worksheet)
'
' File layout orientation
' Keyboard Shortcut: Ctrl+Shift+L
    Dim i As Long
    Application.PrintCommunication = False
    With ws.PageSetup
        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .HeaderMargin = Application.InchesToPoints(0.3)
        .FooterMargin = Application.InchesToPoints(0.3)
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
        .PaperSize = xlPaperLegal
    End With
    On Error GoTo errhandler
    Application.PrintCommunication = True
    On Error GoTo 0
    Exit Sub
    
errhandler:
    i = i + 1
    If i < 5 Then
        Resume
    Else
        Resume Next
    End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.