VBA – 页面设置macros不对工作表进行更改

这对我来说很奇怪

我在VBA中编写了一个macros来遍历工作簿中的所有工作表,并以特定的方式设置它们 – 现在,如果我一步一步地运行macros,但是当我让它自动运行时,并非所有更改都会生效。

我的macros的一个简单版本如下:

Sub SetUpPage() Dim wks As Worksheet If Application.Version >= 14 Then Application.PrintCommunication = False End If For Each wks In ActiveWorkbook.Sheets wks.PageSetup.PrintArea = wks.UsedRange.Address With wks.PageSetup .PaperSize = xlPaper11x17 .Orientation = xlPortrait .Order = xlDownThenOver .Zoom = 80 .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .TopMargin = Application.InchesToPoints(0.25) .BottomMargin = Application.InchesToPoints(0.25) .HeaderMargin = Application.InchesToPoints(0.3) .FooterMargin = Application.InchesToPoints(0.3) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Draft = False .FirstPageNumber = xlAutomatic .BlackAndWhite = False .PrintErrors = xlPrintErrorsDisplayed End With Next wks If Application.Version >= 14 Then Application.PrintCommunication = True End If End Sub 

…看起来主要cuplrit(但不是唯一的)是它没有正确设置.PaperSize = xlPaper11x17

我以为我可能是Application.PrintCommunication = False ,所以我评论这些线,但仍然是同样的问题…我甚至尝试在执行期间激活所需的工作表,认为这可能是问题…仍然没有运气!

…通过,我得到一组(预期)的结果,让macros运行,我得到另一个。

我在Win 7 x64上使用Excel 2007。

有什么想法吗? 任何我失踪?

谢谢!!

我可以解决这个问题的最好方法 – 仍然对我来说并不完美 – 就是围绕正在设置的属性的顺序移动….

它似乎做了很大的改变(虽然我不知道为什么)改变顺序:

 With wks.PageSetup .Zoom = 80 .Order = xlDownThenOver .Orientation = xlPortrait .PaperSize = xlPaper11x17 .PrintQuality = 600 .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .CenterHorizontally = False .CenterVertically = False .Draft = False .FirstPageNumber = xlAutomatic .BlackAndWhite = False .PrintErrors = xlPrintErrorsDisplayed .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .TopMargin = Application.InchesToPoints(0.25) .BottomMargin = Application.InchesToPoints(0.25) .HeaderMargin = Application.InchesToPoints(0.3) .FooterMargin = Application.InchesToPoints(0.3) End With 

我也执行了@guitarthrowersbuild议从上面的评论:

 ActiveWorkbook.Sheets.Select With ActiveSheet.PageSetup .... End With 

但是这与这个具体的问题没有太大的关系……但是,它确实提供了比单独循环每个工作表明确的性能收益。

希望这pipe理帮助别人….