excel - 页面设置宏未对工作表进行所有更改

标签 excel vba

我在 VBA 中编写了一个宏来循环遍历工作簿中的所有工作表并以特定方式设置它们。

如果我单步执行宏,一切都运行良好,但是当我让它自动运行时,并非所有更改都会生效。

我的宏的精简版本如下:

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

主要但不是唯一的问题是它没有正确设置 .PaperSize = xlPaper11x17

我想我可能是Application.PrintCommunication = False,所以我注释掉了这些行,但仍然是同样的问题。

我尝试在执行期间激活所需的工作表。

我使用的是 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

我还实现了上述评论中的@guitarthrowers 建议:

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

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

希望这能够帮助其他人......

关于excel - 页面设置宏未对工作表进行所有更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727995/

相关文章:

php - 格式化查询的 csv 输出

vba - 如果我后面不写 "Dim",用 "as something"声明变量就没用了吗?

.net - 此代码适用于 VB,但在 VB.NET 中会出现异常,为什么?

javascript - 使用 ActiveXOjbect 和 javascript 保存 Excel 工作表

excel - 在 VBA 中使用 Range.Find 仅查找前一个值 x?

excel - 如何将文本附加到单元格并保持格式?

mysql - 从 EXCEL 连接到 SQL 数据库

vba - Excel VBA 内存不足

excel - 删除工作表中的所有空格

excel - 如何创建一个宏来查找、选择和剪切列,然后将它们粘贴到另一个工作表中?