每隔第n行Excel VBA插入页面中断

时间:2017-03-10 12:07:02

标签: excel vba excel-vba

我有以下代码可以执行所有操作,除了在第35行之后插入分页符。有人可以帮忙吗?

Sub PrintSet()

    Dim rs As Worksheet
    Dim sPrintArea As String

    sPrintArea = "A1:AE65"
    For Each rs In Sheets
        rs.PageSetup.Orientation = xlLandscape
        rs.PageSetup.Zoom = False
        rs.PageSetup.FitToPagesWide = 1
        rs.PageSetup.FitToPagesTall = 2
        rs.PageSetup.PrintArea = sPrintArea
        rs.HPageBreaks.Add before:=Range("A36")                               
     Next rs

End Sub

3 个答案:

答案 0 :(得分:2)

重写最后一行:

rs.HPageBreaks.Add before:=rs.Range("A36")

因此,您还要添加引用rs。它应该更好。了解Microsoft关于引用范围的说法: https://msdn.microsoft.com/en-us/library/office/aa221547(v=office.11).aspx

答案 1 :(得分:0)

尝试这样的事情:

Dim hpgbr As HPageBreak
Dim hpgbrs As HPageBreaks
Set hpgbr = hpgbrs.Add(Before:=Range("A36"))

答案 2 :(得分:0)

这对我有用。我删除了缩放

Sub PrintSet()
Dim rs As Worksheet
Dim sPrintArea As String

    sPrintArea = "A1:AE65"
    For Each rs In Sheets
        rs.PageSetup.Orientation = xlLandscape
        rs.PageSetup.FitToPagesWide = 1
        rs.PageSetup.FitToPagesTall = 2
        rs.PageSetup.PrintArea = sPrintArea
        rs.HPageBreaks.Add before:=Range("A36")
    Next rs
End Sub