Insert page breaks in Excel to split data for printing .pdf

前端 未结 1 877
臣服心动
臣服心动 2021-01-07 01:43

I am working with an Excel file that can contains dynamic rows. I export it to .pdf and it works well. The matter is that all the content is on a single page in the .pdf fil

相关标签:
1条回答
  • 2021-01-07 02:22

    You can setup the page breaks like this :

    Worksheets("Sheet1").HPageBreaks.Add Before:=Worksheets("Sheet1").Rows(25)
    Worksheets("Sheet1").VPageBreaks.Add Before:=Worksheets("Sheet1").Columns("J")
    
    Worksheets("Sheet1").Rows(25).PageBreak = xlPageBreakManual
    Worksheets("Sheet1").Columns("J").PageBreak = xlPageBreakManual
    

    FYI, I didn't managed to make the Range.PageBreak to work

    Or for a range, something like this :

    With Range(blabla)
        .Rows(.Rows.Count).EntireRow.PageBreak = xlPageBreakManual
        '.Rows(Int(.Rows.Count/2)).EntireRow.PageBreak = xlPageBreakManual
        .Columns(.Columns.Count).EntireColumn.PageBreak = xlPageBreakManual
    End With
    

    Here is your corrected code :

    Sub test_Wanceslas()
    Dim Ws As Worksheet, _
        Rg As Range, _
        LastRow As Long, _
        Count As Long, _
        ii As Long
    
    ii = 21 ' first page break
    Set Ws = ActiveSheet
    
    With Ws
        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
        Count = LastRow
        Set Rg = .Range("A1", "K" & Count) 'The range of the document
    
        If LastRow > 30 Then ' count is the number of row. Break at every 15 rows
            .ResetAllPageBreaks
            .PageSetup.PrintArea = Rg.Address
    
            While Count > 0 And ii < LastRow
                If Count > 15 Then ' no page break if there is less than 15 rows left
                    '.Rows(ii).PageBreak = xlPageBreakManual
                    .HPageBreaks.Add Before:=.Rows(ii)
                End If
                ii = ii + 15
                Count = Count - 15
            Wend
        End If
    End With
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题