Ignoring blank or "zero" rows when printing - Help with print macro

TheFailbird

New Member
Joined
Oct 15, 2014
Messages
12
I'm trying to create a macro that will, when printing, ignore the rows in my spreadsheet that either contain zeros or are blank; the spreadsheet is large and I don't want to print pages of rows with useless information.

I have an older version of the workbook from 2007 that contains such a macro - it's activated by a command button and works like a charm. Replicating the code in my newer version (and using Excel 2010), however, I find that the code does not function. Can anyone help me:

1. Analyze the following code and suggest probable issues?​

2. Help me understand why the original author of this macro dim'd "cell" as "range"? I guess I'm trying to understand why the author expressed "cell" as Range, but simultaneously assigned "rng" as Range.​

3. Explain what "Set rng = Columns(9).SpecialCells(xlFormulas, xlNumbers)" does, and why it only seems to encompass column 9, instead of the whole spreadsheet?​

I am a novice and don't know that much about VBA, so please try to be concise, if possible.

Code:
Sub HideRows()


Dim cell As Range, rng As Range
Cells.Rows.Hidden = False
On Error Resume Next
Set rng = Columns(9).SpecialCells(xlFormulas, xlNumbers)
On Error GoTo 0
For Each cell In rng
If cell.Value = 0 Then
   cell.EntireRow.Hidden = True
End If
Next
End Sub

P.S. I am activating the above code with a command button in my spreadsheet.

Thank you for your help!


--TheFailbird--
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Thanks for all your help, everyone! (/sarcasm) I was able to figure it out. The above code DOES actually work in Excel 2010, in my newer workbook. I believe the reason it wasn't working before was because my newer spreadsheet didn't have data in Column 9. Novice mistake (Keep in mind I'm very new with VBA)!

Still, the above code is good if anyone wants to hide rows when a column contains "zero" entries.

--TheFailbird--
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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