Creating Workbooks with Page Breaks

UT_CL

New Member
Joined
Mar 27, 2024
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
I have a spreadsheet with data that are separated by page breaks. I'm trying to figure out a VBA script that:
  • create workbooks after each page breaks
  • save in the same folder as the current spreadsheet with the naming convention (ex. Workbook 1, Workbook 2, etc.)
  • each workbook needs the same first two rows
Thanks in advance for any advice.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this macro, which copies the rows using Copy and Paste/PasteSpecial to preserve cell formats, row heights and column widths in the new workbooks.

VBA Code:
Public Sub Copy_Each_Page_Break_Section_To_New_Workbook()

    Dim dataWorksheet As Worksheet
    Dim saveActiveCell As Range
    Dim saveInFolder As String
    Dim originalView As XlWindowView
    Dim lastRow As Long, pageStartRow As Long
    Dim page As Long
    
    saveInFolder = ActiveWorkbook.Path & "\"
    
    'Look on the active sheet in the active workbook
    
    Set dataWorksheet = ActiveWorkbook.ActiveSheet
    Set saveActiveCell = ActiveCell
    
    Application.ScreenUpdating = False
    
    'Save current view and change it to Page Break Preview
    
    With ActiveWindow
        originalView = .View
        .View = xlPageBreakPreview
    End With
    
    With dataWorksheet
    
        lastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row        
        pageStartRow = 1
        
        'Copy rows before each page break to a new workbook
        
        For page = 1 To .HPageBreaks.Count
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, dataWorksheet.HPageBreaks(page).Location.Row - 1, saveInFolder & "Workbook " & page & ".xlsx"
            pageStartRow = .HPageBreaks(page).Location.Row
        Next
    
        'Copy rows after last page break, if any, to a new workbook
        
        If pageStartRow <= lastRow Then
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, lastRow, saveInFolder & "Workbook " & page & ".xlsx"
        End If
    
    End With
    
    'Restore original view
    
    ActiveWindow.View = originalView
    
    'Restore active cell
    
    dataWorksheet.Activate
    saveActiveCell.Select
    
    Application.ScreenUpdating = True

    MsgBox "Done"
    
End Sub


Private Sub Copy_Page_Rows_To_New_Workbook(page As Long, dataWorksheet As Worksheet, pageStartRow As Long, pageEndRow As Long, newWorkbookFullName As String)
    
    Dim newWorkbook As Workbook
    
    Set newWorkbook = Workbooks.Add(xlWBATWorksheet)
    
    With newWorkbook.Worksheets(1)
        .Name = dataWorksheet.Name
        If page = 1 Then
            'First page, so copy rows for page 1, which include rows 1:2, to destination sheet
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A1")
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        Else
            'Subsequent page, so copy rows 1:2 to destination sheet, followed by the rows for this page
            dataWorksheet.Rows("1:2").Copy .Range("A1")
            dataWorksheet.Rows("1:2").Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A3")
        End If
        .Range("A1").Select
    End With
        
    Application.DisplayAlerts = False 'suppress warning if file exists
    newWorkbook.SaveAs newWorkbookFullName, FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    newWorkbook.Close False
                
End Sub
 
Upvote 0
Solution
Try this macro, which copies the rows using Copy and Paste/PasteSpecial to preserve cell formats, row heights and column widths in the new workbooks.

VBA Code:
Public Sub Copy_Each_Page_Break_Section_To_New_Workbook()

    Dim dataWorksheet As Worksheet
    Dim saveActiveCell As Range
    Dim saveInFolder As String
    Dim originalView As XlWindowView
    Dim lastRow As Long, pageStartRow As Long
    Dim page As Long
   
    saveInFolder = ActiveWorkbook.Path & "\"
   
    'Look on the active sheet in the active workbook
   
    Set dataWorksheet = ActiveWorkbook.ActiveSheet
    Set saveActiveCell = ActiveCell
   
    Application.ScreenUpdating = False
   
    'Save current view and change it to Page Break Preview
   
    With ActiveWindow
        originalView = .View
        .View = xlPageBreakPreview
    End With
   
    With dataWorksheet
   
        lastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row       
        pageStartRow = 1
       
        'Copy rows before each page break to a new workbook
       
        For page = 1 To .HPageBreaks.Count
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, dataWorksheet.HPageBreaks(page).Location.Row - 1, saveInFolder & "Workbook " & page & ".xlsx"
            pageStartRow = .HPageBreaks(page).Location.Row
        Next
   
        'Copy rows after last page break, if any, to a new workbook
       
        If pageStartRow <= lastRow Then
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, lastRow, saveInFolder & "Workbook " & page & ".xlsx"
        End If
   
    End With
   
    'Restore original view
   
    ActiveWindow.View = originalView
   
    'Restore active cell
   
    dataWorksheet.Activate
    saveActiveCell.Select
   
    Application.ScreenUpdating = True

    MsgBox "Done"
   
End Sub


Private Sub Copy_Page_Rows_To_New_Workbook(page As Long, dataWorksheet As Worksheet, pageStartRow As Long, pageEndRow As Long, newWorkbookFullName As String)
   
    Dim newWorkbook As Workbook
   
    Set newWorkbook = Workbooks.Add(xlWBATWorksheet)
   
    With newWorkbook.Worksheets(1)
        .Name = dataWorksheet.Name
        If page = 1 Then
            'First page, so copy rows for page 1, which include rows 1:2, to destination sheet
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A1")
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        Else
            'Subsequent page, so copy rows 1:2 to destination sheet, followed by the rows for this page
            dataWorksheet.Rows("1:2").Copy .Range("A1")
            dataWorksheet.Rows("1:2").Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A3")
        End If
        .Range("A1").Select
    End With
       
    Application.DisplayAlerts = False 'suppress warning if file exists
    newWorkbook.SaveAs newWorkbookFullName, FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    newWorkbook.Close False
               
End Sub
Thank you! Thank you John! It worked seamlessly. I'm very new to VBA so this is way more advance than what I was trying to do on my own.
 
Upvote 0
What can I do to change the Save File Name to what is in cell A4 of each workbook and today's date instead of Workbook 1, Workbook 2 and so? It should save the file as 'Cell A4 today's date' (Ex. ABC 04.18.24).

I was able to add the date part by inputting "Dim dDate As String" and "dDate = Format(Now, "mm.dd.yy")". Then, change "saveInFolder & "Workbook " & page & dDate & ".xlsx". But, I want to remove "Workbook & page" and replace it with whatever title is in cell A4. Thanks for any help in advance.
 
Upvote 0
What can I do to change the Save File Name to what is in cell A4 of each workbook and today's date instead of Workbook 1, Workbook 2 and so? It should save the file as 'Cell A4 today's date' (Ex. ABC 04.18.24).

Replace the whole Copy_Page_Rows_To_New_Workbook routine with:
VBA Code:
Private Sub Copy_Page_Rows_To_New_Workbook(page As Long, dataWorksheet As Worksheet, pageStartRow As Long, pageEndRow As Long, newWorkbookFolder As String)
    
    Dim newWorkbook As Workbook
    Dim filename As String
    
    Set newWorkbook = Workbooks.Add(xlWBATWorksheet)
    
    With newWorkbook.Worksheets(1)
        .Name = dataWorksheet.Name
        If page = 1 Then
            'First page, so copy rows for page 1, which include rows 1:2, to destination sheet
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A1")
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        Else
            'Subsequent page, so copy rows 1:2 to destination sheet, followed by the rows for this page
            dataWorksheet.Rows("1:2").Copy .Range("A1")
            dataWorksheet.Rows("1:2").Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A3")
        End If
        .Range("A1").Select
        filename = .Range("A4").Value & Format(Date, " mm.dd.yy") & ".xlsx"
    End With
        
    Application.DisplayAlerts = False 'suppress warning if file exists
    newWorkbook.SaveAs newWorkbookFolder & filename, FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    newWorkbook.Close False
                
End Sub

And change the call to it with this:
VBA Code:
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, dataWorksheet.HPageBreaks(page).Location.Row - 1, saveInFolder
 
Upvote 0
Replace the whole Copy_Page_Rows_To_New_Workbook routine with:
VBA Code:
Private Sub Copy_Page_Rows_To_New_Workbook(page As Long, dataWorksheet As Worksheet, pageStartRow As Long, pageEndRow As Long, newWorkbookFolder As String)
   
    Dim newWorkbook As Workbook
    Dim filename As String
   
    Set newWorkbook = Workbooks.Add(xlWBATWorksheet)
   
    With newWorkbook.Worksheets(1)
        .Name = dataWorksheet.Name
        If page = 1 Then
            'First page, so copy rows for page 1, which include rows 1:2, to destination sheet
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A1")
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        Else
            'Subsequent page, so copy rows 1:2 to destination sheet, followed by the rows for this page
            dataWorksheet.Rows("1:2").Copy .Range("A1")
            dataWorksheet.Rows("1:2").Copy
            With .Range("A1")
                .Select
                .Worksheet.Paste
                .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
            dataWorksheet.Rows(pageStartRow & ":" & pageEndRow).Copy .Range("A3")
        End If
        .Range("A1").Select
        filename = .Range("A4").Value & Format(Date, " mm.dd.yy") & ".xlsx"
    End With
       
    Application.DisplayAlerts = False 'suppress warning if file exists
    newWorkbook.SaveAs newWorkbookFolder & filename, FileFormat:=xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    newWorkbook.Close False
               
End Sub

And change the call to it with this:
VBA Code:
            Copy_Page_Rows_To_New_Workbook page, dataWorksheet, pageStartRow, dataWorksheet.HPageBreaks(page).Location.Row - 1, saveInFolder
Wow I appreciate your assistance, John! Did not know the date can be done this way. This is way easier.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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