VBA hide rows if value in cells is "0" or "(blank)"

mistatasty

New Member
Joined
Jul 21, 2021
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I have a table where I need to hide the rows if the values in A24:A247 are either "0" or have the text "(blank)" in them. For some reason this code only hides rows with 0's and not the "(blank)"s. Anyone know why?

Private Sub Worksheet_Calculate()
Dim c As Range
Application.EnableEvents = False

For Each c In Range("A23:A247")
c.EntireRow.Hidden = c.Value = "(blank)"

Next

For Each c In Range("A23:A247")
c.EntireRow.Hidden = c.Value = "0"

Next

Range("A:B").EntireColumn.AutoFit
Range("D:AN").EntireColumn.AutoFit

For Each c In Range("e23:an23")
c.EntireColumn.Hidden = c.Value = "0"
Next

Application.EnableEvents = True
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
That happens because when you do the first loop when it finds a "(blank)" c.Value = "(blank)" will be TRUE so the row will become "Hidden" as required, then when you do the second loop the same row with a "(blank)" c.Value = "(blank)" this time will be FALSE so the row will once again become "Visible".
If you invert the two loops you will notice that this time the rows with "0" won't be "Hidden".
 
Upvote 0
That happens because when you do the first loop when it finds a "(blank)" c.Value = "(blank)" will be TRUE so the row will become "Hidden" as required, then when you do the second loop the same row with a "(blank)" c.Value = "(blank)" this time will be FALSE so the row will once again become "Visible".
If you invert the two loops you will notice that this time the rows with 0 won't be "Hidden".
what do you mean invert the loops? how would that look?
 
Upvote 0
I mean 'swap' the loops: the first loop becomes second and the second becomes first in the order of the rows of the code.
 
Upvote 0
Sorry, I just noticed that in my post #2 I did a bad Copy/Paste with the test marked [Inline code], should have been:
c.Value = "(blank)" the first and:
c.Value = "0" the second.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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