VBA Code to remove page breaks giving range error HELP

Sweetmusic1

New Member
Joined
Dec 1, 2014
Messages
6
I am unable to figure out how to write the VBA code to ignore page breaks if certain rows are hidden / allow page breaks if rows are not hidden. I am new to VBA code writing. I have checkboxes that when selected the rows will hide or expand when checked. I have the following print command that contains hard page breaks but if a certain selection is hidden the page break still stays. I would appreciate any help in writing this to work properly.

[Range("A7:N146").Select
ActiveSheet.PageSetup.PrintArea = "$A$7:$N$146"
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a27")
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a43")
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a63")
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a91")
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a118")
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Worksheets(1).Range("a139")

If Not ActiveWindow.ActiveSheet.Range("a7:a26").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a27")
End If
If Not ActiveWindow.ActiveSheet.Range("a27:a42").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a43")
End If
If Not ActiveWindow.ActiveSheet.Range("a43:a62").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a63")
End If
If Not ActiveWindow.ActiveSheet.Range("a63:a90").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a91")
End If
If Not ActiveWindow.ActiveSheet.Range("a91:a117").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a118")
End If
If Not ActiveWindow.ActiveSheet.Range("a118:a138").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a139")
End If
If Not ActiveWindow.ActiveSheet.Range("a139:a146").Hidden Then
ActiveSheet.HPageBreaks.Add Before:=Worksheets(1).Range("a147")
End If

ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="pdfFactory Pro", Collate:=True, _
IgnorePrintAreas:=False
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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