I need some more help from you.
The code which you provided works fine no problem for a page break. I need to run the macro for the page break by asking the input file for page break to be done.
For Example, If excel filename "A" contain the code which you have given need to ask to input the filename "B" and process need to be done in file "B".
I have added some code to your code which you provided but it gives error message "1004" "Method 'Range' of object '_Application' failed" at following line :
Set rng = oExcel.Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp))
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 = oExcel.Workbooks.Open(V_fileloc)
oExcel.Worksheets("Sheet1").Activate
oExcel.Sheets("Sheet1").Select
Set rng = oExcel.Range(Cells(2, 2), Cells(Rows.Count, 2).End(xlUp))
On Error Resume Next
oExcel.ResetAllPageBreaks
oExcel.PageSetup.PrintArea = ""
For Each cl In rng
If cl.Value <> cl.Offset(1, 0).Value Then
oExcel.HPageBreaks.Add Before:=cl.Offset(1, 0)
End If
Next cl
oWB.Close
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
On Error GoTo 0
End Sub
Bookmarks