我正在尝试自动设置Excel Workbook格式。我已经获得了成功处理目录中所有工作簿的代码。我现在要添加一个用户窗体,该窗体允许用户选择他们想要的格式选项。这样的例子之一就是页面方向。用户窗体上有一个选项可以选择横向或纵向。选择一个选项后,将更新PUBLIC变量。
Private Sub OptionButton2_Click()
OPTOrientation = "xlLandscape"
End Sub
我更新了多个页面设置属性,因此我使用With.ActiveSheet.PageSetup,然后将它们列出。当我尝试将OPTOrientation分配给“页面设置”属性.Orientation时:
.Orientation = OPTOrientation
但是,这会产生以下错误:无法设置PageSetup类的Orientation属性。当我手动键入“ xlPortrait”而不是引用变量“ OPTOrientation”时,代码将起作用。我是否可以使用公共变量将值分配给类属性?我的数据类型错误吗?
扭曲之处在于PageSetup对象的[.PrintHeadings]属性是一个布尔值,我可以为该属性分配一个变量以解决问题。我在下面的示例中做了。
这是宏:
Public OPTOrientation As String
Sub LetterLandscape() '' Macro Name
Dim Headings As Boolean
Headings = False
Sheets.Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = "&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "Page &P of &N"
.RightFooter = ""
.PrintHeadings = Headings
.PrintGridlines = True
.Orientation = OPTOrientation
.papersize = xlPaperLetter
.Order = xlOverThenDown
.FitToPagesWide = 1
.FitToPagesTall = False
.ScaleWithDocHeaderFooter = False
.LeftMargin = Application.InchesToPoints(0.7)
.RightMargin = Application.InchesToPoints(0.7)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
End With
Application.PrintCommunication = True
End Sub