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