Help with VBA to add page breaks that stopped working

rickadams

New Member
Joined
Feb 11, 2018
Messages
31
I have a Macro that was supplied to me earlier in 2018 from this group, and it will not work any more.
I get the following error.
Run Time Error 1004 Application-Defined or Object-Defined Error.
Sub insertpagebreaks()
' this macro adds page breaks if data in column A changes for printing out new page for each time there is a change.
Dim I As Long, J As Long
J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For I = J To 2 Step -1
If Range("A" & I).Value <> Range("A" & I - 1).Value Then
ActiveSheet.HPageBreaks.Add Before:=Range("A" & I) '<----- This is the line of code that shows the error in Debug
End If
Next I
End Sub


THANK YOU
 

Attachments

  • add page breaks error.jpg
    add page breaks error.jpg
    30.7 KB · Views: 10

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I ran your code as-is and it worked fine for me. No errors. You might try changing the implied range references to explicit definitions as I have done below, and see if that makes a difference.

VBA Code:
Private Sub insertpagebreaks()
' this macro adds page breaks if data in column A changes for printing out new page for each time there is a change.
    Dim I As Long, J As Long
    
    J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

    For I = J To 2 Step -1
        If ActiveSheet.Range("A" & I).Value <> ActiveSheet.Range("A" & I - 1).Value Then
            ActiveSheet.HPageBreaks.Add Before:=ActiveSheet.Range("A" & I)    '<----- This is the line of code that shows the error in Debug
        End If
    Next I

End Sub

(Tip: when posting your code, please try to use 'code tags' to format the code as I have done above


as it makes the code easier to read.)
 
Upvote 1
Solution
I ran your code as-is and it worked fine for me. No errors. You might try changing the implied range references to explicit definitions as I have done below, and see if that makes a difference.

VBA Code:
Private Sub insertpagebreaks()
' this macro adds page breaks if data in column A changes for printing out new page for each time there is a change.
    Dim I As Long, J As Long
   
    J = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

    For I = J To 2 Step -1
        If ActiveSheet.Range("A" & I).Value <> ActiveSheet.Range("A" & I - 1).Value Then
            ActiveSheet.HPageBreaks.Add Before:=ActiveSheet.Range("A" & I)    '<----- This is the line of code that shows the error in Debug
        End If
    Next I

End Sub

(Tip: when posting your code, please try to use 'code tags' to format the code as I have done above


as it makes the code easier to read.)
Thank you So much! That worked perfectly... Also
it says If you received at least one reply answering your question and solving your problem,
then please remember to click the "Mark as solution" button - - right next to the best post which is answering your question.
I would like to mark as solution button but it is not there so I am not able to do that.
 
Upvote 0
Thank you So much! That worked perfectly... Also
it says If you received at least one reply answering your question and solving your problem,
then please remember to click the "Mark as solution" button - - right next to the best post which is answering your question.
I would like to mark as solution button but it is not there so I am not able to do that.
I was able to mark as a solution however the option was not there until after I posted my reply, Thank you again!
 
Upvote 0
In future please mark the post that contains the solution, rather than your post saying it works.
I have done that for you this time.
 
Upvote 1

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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