Hide Rows Based on Formula Result

PHIL.Pearce84

Board Regular
Joined
May 16, 2011
Messages
152
Office Version
  1. 365
Platform
  1. Windows
Hi

I dont know much about VBA but can anyone help?

I need a macro that will hide all rows where the formula result is zero as a result of the cost centre selected from a validation list?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the forums!

Why not autofilter the data to show only non-zero values?
 
Upvote 0
Try like this: it checks A1:A10 for zeroes. Right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
For i = 1 To 10
    Rows(i).Hidden = Range("A" & i).Value = 0
Next i
End Sub
 
Upvote 0
Hi

I changed the column to Column M and the rows from 16 to 102 and this works but it seems to keep running until I press esc? Also is there anyway for it to not hide row 94?

Thanks for such a quick response
 
Upvote 0
Try

Code:
Private Sub Worksheet_Calculate()
Dim i As Long
Application.EnableEvents = False
For i = 16 To 102
    If i <> 94 Then
        Rows(i).Hidden = Range("M" & i).Value = 0
    End If
Next i
Application.EnableEvents = True
End Sub
 
Upvote 0
Perfect thank you very much :)

Just out of curiousity should I need to avoid hiding another row at a later date what would I have to do?
 
Upvote 0
Try like this

Rich (BB code):
Private Sub Worksheet_Calculate()
Dim i As Long
Application.EnableEvents = False
For i = 16 To 102
    If i <> 94 And i <> 96 Then
        Rows(i).Hidden = Range("M" & i).Value = 0
    End If
Next i
Application.EnableEvents = True
End Sub
 
Upvote 0
To expand on this a bit to prevent excessive AND statements:

Code:
Private Sub Worksheet_Calculate()
Dim i           As Long, _
    ExcludeRows As Variant
    
Application.EnableEvents = False
ExcludeRows = Array(94, 96) 'add row numbers here to exclude
For i = 16 To 102
    If Not IsError(Application.Match(i, ExcludeRows, 0)) Then
        Rows(i).Hidden = Range("M" & i).Value = 0
    End If
Next i
Application.EnableEvents = True
End Sub
 
Upvote 0
Hi Guys the Macro above has suddenly stopped working and I can see why? It has been fine but just stopped I and tried to repaste it but to no avail :( Any ideas?
 
Upvote 0
Can you please copy/paste the macro you are using here? Have you changed the spreadsheet in any way?
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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