Clear & Reset Page Breaks On Visible Rows Only for Printing & Ignore Hidden Rows

bearwires

Board Regular
Joined
Mar 25, 2008
Messages
57
Office Version
  1. 365
Platform
  1. Windows
I have a document which has 76 pages.

All 76 pages are on 1 worksheet called "COSHH"

Each page is made up of a standard template form, same number of rows & columns for each page.

Each form page is blank unless validation drop down lists on other worksheets are selected which then populates the respective form/rows using IF function, pulling data from a completely separate worksheet.

Each validation drop down cell on each of the other worksheets is linked to its own template form/page.

If a drop down item is selected, data is pulled from the other worksheets to populate the respective form in column B of the COSHH worksheet.

Column A cells have the form headings and is blank "" unless column B is populated using IF function.

I have a macro which hides all blank rows (forms) if they have no data. This macro works great, thanks to JohnnyL here on MrExcel.


The problem is, when I go to print preview it is showing all 76 pages, most of which are blank even though they are hidden on the worksheet and not visible in print layout / normal layout / etc.

I have tried clearing all page breaks and setting them again just in case that was an issue, but when I hide all the blank rows, it stills shows all 76 pages in the print preview so it didnt work.


If I hide all the empty forms / rows, then clear all page breaks, then reset the page breaks for just the cells that are not hidden, this will work and it will only show the forms I need to see in the print preview, which is what I want the end result to be.


Is there a way to use some VBA code to clear & reset the page breaks, then insert new page breaks on the non-hidden rows?
The hiding part is sorted, it is just the resetting & re-inserting of new page breaks to rows that are not hidden I am looking to achieve.

Page 1 starts at row 5

Row 5 - Row 33 is Page 1
Row 34 - Row 62 is Page 2
Row 63 - Row 91 is Page 3
Row 92 - Row 120 is Page 4
ETC

Cells B32, B61, B90, B119.......B2207 will contain a text string if the page is to be visible / printed. These same cells will be "" blank if the page is hidden from view / not to be printed.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have a document which has 76 pages.

All 76 pages are on 1 worksheet called "COSHH"

Each page is made up of a standard template form, same number of rows & columns for each page.

Each form page is blank unless validation drop down lists on other worksheets are selected which then populates the respective form/rows using IF function, pulling data from a completely separate worksheet.

Each validation drop down cell on each of the other worksheets is linked to its own template form/page.

If a drop down item is selected, data is pulled from the other worksheets to populate the respective form in column B of the COSHH worksheet.

Column A cells have the form headings and is blank "" unless column B is populated using IF function.

I have a macro which hides all blank rows (forms) if they have no data. This macro works great, thanks to JohnnyL here on MrExcel.


The problem is, when I go to print preview it is showing all 76 pages, most of which are blank even though they are hidden on the worksheet and not visible in print layout / normal layout / etc.

I have tried clearing all page breaks and setting them again just in case that was an issue, but when I hide all the blank rows, it stills shows all 76 pages in the print preview so it didnt work.


If I hide all the empty forms / rows, then clear all page breaks, then reset the page breaks for just the cells that are not hidden, this will work and it will only show the forms I need to see in the print preview, which is what I want the end result to be.


Is there a way to use some VBA code to clear & reset the page breaks, then insert new page breaks on the non-hidden rows?
The hiding part is sorted, it is just the resetting & re-inserting of new page breaks to rows that are not hidden I am looking to achieve.

Page 1 starts at row 5

Row 5 - Row 33 is Page 1
Row 34 - Row 62 is Page 2
Row 63 - Row 91 is Page 3
Row 92 - Row 120 is Page 4
ETC

Cells B32, B61, B90, B119.......B2207 will contain a text string if the page is to be visible / printed. These same cells will be "" blank if the page is hidden from view / not to be printed.
With alot of googling and trial & error, I managed to code something which achieves what I wanted.
Here is the code just in case it can help someone else:

VBA Code:
Sub PageBreaksToVisibleRows()
'
lr As Long, k As Long
  Dim Cell As Range
  
  Const RowsPerPageBreak As Long = [COLOR=rgb(226, 80, 65)]29[/COLOR]
  
  lr = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, SearchFormat:=False).Row
  With Sheets("[COLOR=rgb(226, 80, 65)]Sheet 1[/COLOR]").ResetAllPageBreaks
  
For Each Cell In Range("A[COLOR=rgb(226, 80, 65)]5[/COLOR]:A" & lr).SpecialCells(xlVisible)
    k = k + 1
    If k = RowsPerPageBreak + 1 Then
      ActiveSheet.HPageBreaks.Add Before:=Cell
      k = 1
    
End If
  Next Cell
End With

End Sub

Just amend the red items to suit your requirements.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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