VBA to show/hide rows

stephen.smith

Board Regular
Joined
Jul 7, 2010
Messages
119
Hey Guys I am trying to show or hide rows in a spreadsheet based on the value in a cell. The range is cells B4:B532, if the cell in column B in any given row in the range has a value of OK i want the entire row to hide but if the cell has a value of ALERT i want the entire row to be seen. Can anyone help with code to do this.
Thanks in advance
Stephen
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I have to run a macro on this page to update some references and change some values so if I can add some code in that will do this also then I would be happy to do this.
Thanks
 
Upvote 0
Something along the lines of

Dim c As Range, rng
Set rng = Range("b4:b532")
For Each c In rng
If c.Value = "Ok" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next c
 
Upvote 0
You could use:

Code:
Sub HideRows()

Dim c As Range, rng As Range
Dim checkVal As String

checkVal = "Ok"
Set rng = Range("B4:B532")

    For Each c In rng.Cells

    If c.Value = "Ok" Then
    c.EntireRow.Hidden = True
    Else
    c.EntireRow.Hidden = False
    End If

    Next c         

End Sub
 
Upvote 0
Hi Adam
Sorry but this doesnt work either, nothing gets hidden. Just so you know all cells in column B are referenced to cells in another worksheet which hold a value of either "ok" or "alert". will this make a difference?
 
Upvote 0
Hmm... Try:

Code:
Sub HideRows()  
Dim c As Range, rng As Range 
Dim checkVal As String  

checkVal = "ok" 
Set rng = Range("B4:B532")      

For Each c In rng.Cells      

If c.Value = checkVal Then     
c.EntireRow.Hidden = True     
Else     
c.EntireRow.Hidden = False     
End If      
Next c           

End Sub
Now you can just change the variable checkVal to the relevant entry that you want to hide for. Its most likely because VBA code is case-sensitive so 'Ok' is different to 'ok'.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,876
Members
452,949
Latest member
Dupuhini

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