Make a new worksheet at every page break in a worksheet

teachman

Active Member
Joined
Aug 31, 2011
Messages
321
Hello,

I am a gov't employee and as such any documentation or spreadsheet tool I develop has to be something called '508 compliant'. Spreadsheets aren't so bad but when I create a report with page breaks in a spreadsheet that I save as a PDF file, 508 requires every page break to be the end of a chapter in the PDF. Meaning, if I have an Excel report that documents several different aspects of the spreadsheet use as a tool currently I just insert a page break so when users print out the worksheet they get separate pages with headers and footers that I suppose could be considered chapters. I have one project that has about 600 separate sections, describing database tables and columns and choice lists, etc. Each section is separated by a page break. I'm being told that each of the 600 or so separate sections must be a separate worksheet so the PDF tool used to make Excel documents into PDFs can make a chapter out of each section. Each worksheet is considered a chapter.

I'm looking for help so that I can include in my vba scripts that create the main document I can add some code that will create a worksheet and move/copy the section from the main worksheet to the newly created worksheet.

Thanks in advance for any guidance or suggestions.

George Teachman
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this. The code operates on the active sheet in the active workbook, allowing you to run it on any open workbook, and copies rows to new worksheets. The new worksheets are named "Page 1", "Page 2", etc., although it doesn't check whether the named sheets already exist, so the code will crash if this is the case.
Code:
Public Sub Copy_Each_Page_Break_Section_To_New_Worksheet()

    Dim reportWorksheet As Worksheet
    Dim saveActiveCell As Range
    Dim lastRow As Long, pageStartRow As Long
    Dim page As Long
    Dim newWorksheet As Worksheet
    
    'Look on the active sheet in the active workbook
    
    Set reportWorksheet = ActiveWorkbook.ActiveSheet
    Set saveActiveCell = ActiveCell
    
    With reportWorksheet
        
        lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        pageStartRow = 1
        
        'Copy rows in each page break section to new worksheet
        
        For page = 1 To .HPageBreaks.Count            
            Set newWorksheet = .Parent.Worksheets.Add(after:=.Parent.Worksheets(.Parent.Worksheets.Count))
            newWorksheet.Name = "Page " & page
            .Rows(pageStartRow & ":" & .HPageBreaks(page).Location.Row - 1).EntireRow.Copy newWorksheet.Range("A1")            
            pageStartRow = .HPageBreaks(page).Location.Row
        Next
    
        If pageStartRow <= lastRow Then
        
            'Copy rows after last page break to new worksheet
            
            Set newWorksheet = .Parent.Worksheets.Add(after:=.Parent.Worksheets(.Parent.Worksheets.Count))
            newWorksheet.Name = "Page " & page
            .Rows(pageStartRow & ":" & lastRow).EntireRow.Copy newWorksheet.Range("A1")
        
        End If
    
    End With
    
    'Restore active cell
    
    reportWorksheet.Activate
    saveActiveCell.Select
    
End Sub
 
Upvote 0
Try this. The code operates on the active sheet in the active workbook, allowing you to run it on any open workbook, and copies rows to new worksheets. The new worksheets are named "Page 1", "Page 2", etc., although it doesn't check whether the named sheets already exist, so the code will crash if this is the case.
Code:
Public Sub Copy_Each_Page_Break_Section_To_New_Worksheet()

    Dim reportWorksheet As Worksheet
    Dim saveActiveCell As Range
    Dim lastRow As Long, pageStartRow As Long
    Dim page As Long
    Dim newWorksheet As Worksheet
   
    'Look on the active sheet in the active workbook
   
    Set reportWorksheet = ActiveWorkbook.ActiveSheet
    Set saveActiveCell = ActiveCell
   
    With reportWorksheet
       
        lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        pageStartRow = 1
       
        'Copy rows in each page break section to new worksheet
       
        For page = 1 To .HPageBreaks.Count           
            Set newWorksheet = .Parent.Worksheets.Add(after:=.Parent.Worksheets(.Parent.Worksheets.Count))
            newWorksheet.Name = "Page " & page
            .Rows(pageStartRow & ":" & .HPageBreaks(page).Location.Row - 1).EntireRow.Copy newWorksheet.Range("A1")           
            pageStartRow = .HPageBreaks(page).Location.Row
        Next
   
        If pageStartRow <= lastRow Then
       
            'Copy rows after last page break to new worksheet
           
            Set newWorksheet = .Parent.Worksheets.Add(after:=.Parent.Worksheets(.Parent.Worksheets.Count))
            newWorksheet.Name = "Page " & page
            .Rows(pageStartRow & ":" & lastRow).EntireRow.Copy newWorksheet.Range("A1")
       
        End If
   
    End With
   
    'Restore active cell
   
    reportWorksheet.Activate
    saveActiveCell.Select
   
End Sub
Hi
Thank you for the code, it works great however, Im hoping you can help with an additional problem to this. I need each tab to be labeled based on a value within a cell on each new speadsheet. There is a catch, the cell that has the data is always on a different row but only ever in one column ie; H
I look forward to your help :)
 
Upvote 0

Forum statistics

Threads
1,221,572
Messages
6,160,581
Members
451,656
Latest member
SBulinski1975

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top