Excel VBA忽略PageSetup更改

时间:2016-11-02 06:15:46

标签: excel-vba vba excel

我有一个简单的Excel宏(下图),只是将方向从纵向更改为横向。我使用了Excel“记录宏”工具,但是当我使用这个宏时,它忽略了命令。有没有人见过这个?并且,有一个很好的解决方案吗?

Sub Writedown_Format_Landscape()
'
' Writedown_Format_Landscape Macro
Range("A1").Select

Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
End With

Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False

With ActiveSheet.PageSetup
    .LeftHeader = "&D - &T"
    .CenterHeader = "&F"
    .RightHeader = "&P / &N"
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.25)
    .RightMargin = Application.InchesToPoints(0.25)
    .TopMargin = Application.InchesToPoints(0.5)
    .BottomMargin = Application.InchesToPoints(0.25)
    .HeaderMargin = Application.InchesToPoints(0.25)
    .FooterMargin = Application.InchesToPoints(0.25)
    .PrintHeadings = False
    .PrintGridlines = True
    .PrintComments = xlPrintNoComments
    .CenterHorizontally = True
    .CenterVertically = False
    .Orientation = xlLandscape
    .Draft = False
    .PaperSize = xlPaperLetter
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = 100
End With

Range("A1").Select

End Sub

1 个答案:

答案 0 :(得分:0)

整个录制宏中唯一需要的行是:

ActiveSheet.PageSetup.Orientation = xlLandscape

请更改工作表参考,以确保您正在与正确的工作表进行交互。您需要在括号内键入工作表的名称:

Sheets("YourSheetName").PageSetup.Orientation = xlLandscape

现在,如果没有变化,请再次检查打印预览。