
Originally Posted by
saikumar
Welcome.
But heading is not printing in all the pages.
Thanks.
What do you mean? You can't detect whether a cell is a heading or not. You should standardise your workbooks so they either all have headings or don't.
It would also save time if you stated your requirements fully in the first question
Option Explicit
Dim V_fileloc As String
Dim oExcel As Excel.Application
Dim oWB As New Workbook
Sub addPageBreak()
Dim rng As Range
Dim cl As Range
V_fileloc = ""
V_fileloc = Application.GetOpenFilename("Page Break File (*.xls),*.xls", , "Page Break File")
If (V_fileloc = "" Or V_fileloc = "False") Then
MsgBox "Page Break File, Should be selected", vbApplicationModal, "Files"
Exit Sub
End If
' Set oExcel = CreateObject("excel.Application")
Set oWB = Workbooks.Open(V_fileloc)
With oWB.Worksheets("Sheet1")
Select Case MsgBox("Does the sheet have a header row?", vbYesNo Or vbQuestion Or vbDefaultButton1, "Header row")
Case vbYes
Set rng = .Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp))
Case vbNo
Set rng = .Range(Cells(1, 2), Cells(Rows.Count, 2).End(xlUp))
End Select
On Error Resume Next
.ResetAllPageBreaks
.PageSetup.PrintArea = ""
For Each cl In rng
If cl.Value <> cl.Offset(1, 0).Value Then
.HPageBreaks.Add Before:=cl.Offset(1, 0)
End If
Next cl
End With
' oWB.Close
Set oWB = Nothing
On Error GoTo 0
End Sub
Bookmarks