Macro to hide rows that contain a certain value

mrdanielatkins

New Member
Joined
May 21, 2018
Messages
13
Hello All,

I'm diving deeper into Excel than I ever have and honestly, I'm in over my head. I need to write a VBA code that will hide any Row (A20:A80) that contains "Hide this Row" in column "N". I just need this function to work on this one worksheet, but I need it to update as the values update in the column as they may change.

Any help would be greatly a
2aqb9p
ppreciated.


2aqb9p.jpg
[/URL]via Imgflip Meme Generator[/IMG]
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Might be a better way, however this should work.

Code:
Sub HideThisRow()    For i = 20 To 80
    If Range("N" & i) = "Hide This Row" Then Rows(i).EntireRow.Hidden = True
    Next
    End Sub
 
Upvote 0
Nothing happened. I've done very little VBA, so it is probably user error. I just copied and pasted this in the VBA dialog box for the workbook, correct?
 
Upvote 0
but I need it to update as the values update in the column as they may change.
How are the values changing?
Is someone manually updating the values in column N?
If not, what is in column N (if a formula, please post it), and what is driving the changes?
 
Upvote 0
N20:N80 are pulling from a set of buttons on the other worksheets. Here is the code:

=IF('CO# 01'!$I$49=3,"Hide this Row","")
 
Upvote 0
So, how exactly is column I on your 'CO# 1' sheet being updated?
And do the row number between the two sheets correspond (i.e. does updating column I in row 49 update column N on row 49 of your other sheet)?
 
Last edited:
Upvote 0
Each one of the buttons in the Red Rectangle trigger a different value in I-49. There are quite a few references to this cell on each sheet. To answer you other question, No, the row number between the two sheets does not correspond.
2aro9j.jpg
[/URL]
 
Upvote 0
OK, try this.
Go to the sheet with the values in column N where you want to hide the rows, right-click on the sheet tab name at the bottom of the screen, select "View Code", and paste this VBA code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Calculate()
    Dim cell As Range
    For Each cell In Range("N20:N80")
        If UCase(cell.Value) = "HIDE THIS ROW" Then
            Rows(cell.Row).Hidden = True
        Else
            Rows(cell.Row).Hidden = False
        End If
    Next cell
End Sub
This should automatically hide/unhide those rows as the values in column N change.
 
Upvote 0
Thanks for all of your help. I tried the code, it is definitely trying to do something. Ultimately, I'm getting a runtime Error referencing the highlighted line below, then Excel crashes:

Private Sub Worksheet_Calculate()
Dim cell As Range
For Each cell In Range("N20:N80")
If UCase(cell.Value) = "HIDE THIS ROW" Then
Rows(cell.Row).Hidden = True
Else
Rows(cell.Row).Hidden = False
End If
Next cell
End Sub



2arqj7.jpg
[/URL]
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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