Macro to hide/unhide rows based on column value

tlc53

Active Member
Joined
Jul 26, 2018
Messages
399
Hi there,

I've tried a couple of different codes but can't quite pull it together.

In Columns G15:G49 and H15:H49 I have formulas. If both cells in the columns equal zero (eg. G15 & H15), I would like to hide that row (row 15). If one is more than zero, it should be visible. If the data changes later, I would like them to unhide/hide accordingly.

Can someone help me please?

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Add this code to your WorkSheet. (Right click on sheet name-> View Code and paste:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$G$15" Or Target.Address = "$H$15" Then
        Call HideRow
    End If
End Sub
This will look for changes in those 2 cells. If changes are made, it will call HideRow function, which you should place in a new module:
Code:
Sub HideRow()
If Cells(15, "G").Value = "" And Cells(15, "H").Value = "" Then
MsgBox "Cell is Empty."
ActiveSheet.Rows("15").EntireRow.Hidden = True
Else
MsgBox "Not empty"
ActiveSheet.Rows("15").EntireRow.Hidden = False
End If
End Sub
Still, if row is hidden, unless you insert values by formula or macro, you will have to un-hide it manually.
 
Upvote 0
If you hide the row, how are the cells changing?
Do they contain formulae?
 
Upvote 0
Yes, columns G and H contain formulas which transfer figures located on another sheet.

What I would like the marco to do, is unhide all rows and then hide the rows if the formulated values of both G & H are nil/0. If it's easier, I could sum G & H in column I and then hide column I. Then only one column would need to be looked at.

Can it work on formulated cells?
 
Upvote 0
If you put the Sum in col I & use this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range, keyCells As Range
    Set keyCells = Range("I15:I49")
    On Error Resume Next
    Set keyCells = Application.Union(keyCells, keyCells.Precedents)
    On Error GoTo 0
    If Not Application.Intersect(keyCells, Target) Is Nothing Then
      For Each Cl In Range("I15:I49")
        Cl.EntireRow.Hidden = IIf(Cl = 0, True, False)
      Next Cl
    End If
End Sub
It's a trick I recently picked up from @mikerickson.
 
Upvote 0
Thank you. Probably a silly question but, how do I make the code run? When I press play under VBA it just brings up a list of my other marcos..
 
Upvote 0
I now see this code runs if I click in any of the cells in the range, I15:I49. The only thing is, this sheet is used to produce a report and is completely formulated requiring no user input (it collates its information from the previous sheet). So ideally, I would like to be able to prompt when it runs.
Eventually, it will form part of the code which exports the report to pdf automatically, so the user won't ever see this sheet. Only the pdf report which they will run by a macro button on the previous sheet. I've got all that code together. It's just this tidying up of the rows that I'm struggling with and then I somehow need to tie everything altogether (taking one step at a time though!).

I appreciate you/England are currently sleeping :)
 
Upvote 0
In that case, how about
Code:
Sub tlc53()
   Dim Cl As Range
   For Each Cl In Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("I15:I49")
     Cl.EntireRow.Hidden = IIf(Cl = 0, True, False)
   Next Cl
End Sub
Change sheet name to suit
 
Upvote 0
Thanks. However, it's not running. It's coming back with Run-time error '9'. Subscript out of range.

Sub tlc53()
Dim Cl As Range
For Each Cl In Sheets("Sheet81").Range("I15:I49")
Cl.EntireRow.Hidden = IIf(Cl = 0, True, False)
Next Cl
End Sub
 
Last edited:
Upvote 0
That means you don't have a sheet called Sheet81.
If it appears as Sheet81(Somename) in the VBA project window use, it's the codename & used like
Code:
For Each Cl In Sheet81.Range("I15:I49")
Otherwise if it's the name that appears on the sheet tab, check for leading/trailing spaces.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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