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

时间:2014-09-08 15:40:29

标签: 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。

3 个答案:

答案 0 :(得分:3)

我可以解决这个问题的最好方法 - 对我来说仍然没有意义 - 是按照设置属性的顺序移动....

确实将订单更改为:

确实产生了很大的不同(尽管我不知道为什么)
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

但这与这个具体问题没什么关系......但是,它确实提供了明显的性能提升,而不是单独循环每个工作表。

希望这可以帮助别人......

答案 1 :(得分:0)

尽管Application.PrintCommunication旨在加快PageSetup设置的应用,但根据我的经验,有时它会阻止某些设置的应用。希望这对某人有帮助:)

答案 2 :(得分:0)

您需要将PrintCommunication设置为False才能重复标题行

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

然后您可以将PrintCommunication设置为True,以继续其余的PageSetup

Application.PrintCommunication = True
    With ActiveSheet.PageSetup



        .LeftHeader = "&G"
        .LeftHeaderPicture.fileName = ImagePath
        .LeftHeaderPicture.Height = 200
        .LeftHeaderPicture.Width = 80
        .CenterHeader = "&B &22 blah blah"

        .LeftFooter = "&20 &D &T"
        .CenterFooter = "&10 blah blah"
        .RightFooter = "&10 &P of &N"

        .LeftMargin = Application.InchesToPoints(0.5)
        .RightMargin = Application.InchesToPoints(0.5)
        .TopMargin = Application.InchesToPoints(0.75)
        .BottomMargin = Application.InchesToPoints(0.75)
        .HeaderMargin = Application.InchesToPoints(0.1)
        .FooterMargin = Application.InchesToPoints(0.1)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlLandscape
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True



    End With