Don't Print Hidden Rows

DanielleWebb

New Member
Joined
Jul 27, 2005
Messages
36
I have a worksheet with hidden rows and Page Breaks. I know that the page breaks are causing my worksheet to print blank pages where I have page breaks and hidden rows.

I need the page breaks or the form just prints a mess... I tried eliminating them - but that didn't work at all.

I need a macro, which will examine the worksheet, look for the hidden rows, exclude them from the print area, and then Print the worksheet. Can it be done?

Thanks In advance.

(also, on a side note, if you show me the code can you tell me why/how it works? I enjoy the learning!)
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
That is strange. I did find another error corrected in red below. This code does run without error but I'm not sure if it will do what you want.

Rich (BB code):
Sub PrtVis()
Dim pb, i As Integer
pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)
With ActiveSheet
    .PageSetup.PrintArea = .UsedRange.SpecialCells(xlCellTypeVisible).Address
    .PageSetup.Orientation = xlPortrait
    .PageSetup.Zoom = False
    .PageSetup.FitToPagesTall = 1
    For i = LBound(pb) To UBound(pb)
        .HPageBreaks.Add Before:=.Range("A" & pb(i))
    Next i
    .PrintPreview
End With
End Sub

Change .PrintPreview to .PrintOut after testing.
 
Upvote 0
Actually, for the page breaks to work:

Code:
Sub PrtVis()
Dim pb, i As Integer
pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)
With ActiveSheet
    .PageSetup.PrintArea = .UsedRange.SpecialCells(xlCellTypeVisible).Address
    .PageSetup.Orientation = xlPortrait
    For i = LBound(pb) To UBound(pb)
        .HPageBreaks.Add Before:=.Range("A" & pb(i))
    Next i
    .PrintPreview
End With
End Sub
 
Upvote 0
VoG

Would you need to reset all page breaks before adding new one's? Just a thought.

Code:
Sub PrtVis()
Dim pb, i As Integer
pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)
With ActiveSheet
    .ResetAllPageBreaks ' remove all page breaks
    .PageSetup.PrintArea = .UsedRange.SpecialCells(xlCellTypeVisible).Address
    .PageSetup.Orientation = xlPortrait
    For i = LBound(pb) To UBound(pb)
        .HPageBreaks.Add Before:=.Range("A" & pb(i))
    Next i
    .PrintPreview
End With
End Sub
 
Upvote 0
VoG

Would you need to reset all page breaks before adding new one's? Just a thought.

Code:
Sub PrtVis()
Dim pb, i As Integer
pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)
With ActiveSheet
    .ResetAllPageBreaks ' remove all page breaks
    .PageSetup.PrintArea = .UsedRange.SpecialCells(xlCellTypeVisible).Address
    .PageSetup.Orientation = xlPortrait
    For i = LBound(pb) To UBound(pb)
        .HPageBreaks.Add Before:=.Range("A" & pb(i))
    Next i
    .PrintPreview
End With
End Sub

Yes, that's a good point. Thanks.
 
Upvote 0
Also a suggestion is to allow the user to input the amount of copies he/she would like to print...

Code:
Sub PrtVis()
Dim pb, i As Integer
'Dim CopiesCount as Long
'CopiesCount = Application.InputBox("How many copies do you want", Type:=1)

pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)
With ActiveSheet
    .ResetAllPageBreaks ' remove all page breaks
    .PageSetup.PrintArea = .UsedRange.SpecialCells(xlCellTypeVisible).Address
    .PageSetup.Orientation = xlPortrait
    For i = LBound(pb) To UBound(pb)
        .HPageBreaks.Add Before:=.Range("A" & pb(i))
    Next i
'For CopieNumber = 1 To CopiesCount
    .PrintPreview 'Change .PrintPreview to .PrintOut when tested
'Next CopieNumber
End With
End Sub

Of course the .PrintPreview would need to be changed to .PrintOut and activate the CopieNumber arguments and this should work well...
 
Upvote 0
Hi VoG

FYI

I had a look at Danielle's file and have adjusted the code as follows in order for it to meet her desired formatting... I had to mess around a little with the page breaks and zoom to obtain the correct formatting under the different circumstances of the user.

Code:
Sub PrtVis()

Sheet1.Unprotect Password:=""
Dim pb, i As Integer
Dim CopiesCount As Long
CopiesCount = Application.InputBox("How many copies do you want", Type:=1)

pb = Array(100, 181, 262, 343, 424, 505, 586, 667, 748)


With ActiveSheet

    .ResetAllPageBreaks ' remove all page breaks from
    .PageSetup.PrintArea = "$D$1:$W$844"
    .PageSetup.FitToPagesWide = 1
    .PageSetup.Zoom = 75
    .PageSetup.Orientation = xlPortrait
    For i = LBound(pb) To UBound(pb)
   
        If .Rows(pb(i)).Hidden = True Then
        .HPageBreaks.Add Before:=.Cells(749, 1)
        Else
        .HPageBreaks.Add Before:=.Cells(pb(i), 1)
        .HPageBreaks.Add Before:=.Cells(749, 1)
       
        End If
    Next i
For CopieNumber = 1 To CopiesCount
    .PrintPreview 'Change .PrintPreview to .PrintOut when tested
Next CopieNumber
End With
Sheet1.Protect Password:=""
End Sub

If no other object changes are made to the file the code should do the trick.
 
Upvote 0

Forum statistics

Threads
1,215,108
Messages
6,123,134
Members
449,098
Latest member
Doanvanhieu

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