Hide rows based on cell value, continuously

zakavelli

New Member
Joined
Jul 1, 2011
Messages
4
Hi my VBA knowledge is very limited. I am trying to get a macro to hide columns based on a cell value. The cell value is inputted manually and based on this the rows should hide. Basically if the cell d3 is equal to zero then all rows show, but if the value in d3 is greater than 0 then the rows 25:75 are hidden.

I have managed to put together some code based on googling around, and using this forum, and just playing around with code, and luckily for me I do not get any errors.

I've put the VBA into the VBA window by right clicking the sheet tab, and clicking view source using General and Declarations. The code I have used is shown below

Code:
Private Sub Worksheet_Activate()
If Cells(3, 4).Value > 0 Then
  ActiveSheet.Rows("25:75").Hidden = True
ElseIf Cells(3, 4).Value = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub
 
 
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells = "$D$10" > 0 Then
    ActiveSheet.Rows("25:75").Hidden = True
ElseIf Target.Cells = "$D$10" = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub

And I've saved a macro called 'run' in the macro VBA editor window

with the following code:

Code:
Sub run()
If Cells(3, 4) > 0 Then
  ActiveSheet.Rows("25:75").Hidden = True
ElseIf Cells(3, 4) = 0 Then
  ActiveSheet.Rows("25:75").Hidden = False
End If
End Sub

I want the macro to run in the background continuously. Now what happens is if I change the value in d3 from 0 to say '3' nothing happens, where it should hide row 25:75. However I can run the macro manually and it works. but when I then change the value from '3' back to 0 it automatically shows the row 25:75, which it should.

Where it works automatically one way, it doesn't work the other way.


Please can you help?

Thanks,

Zak
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
A minor change gets it working..;)

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$3" Then
        If Target.Value > 0 Then
            ActiveSheet.Rows("25:75").Hidden = True
        Else
            ActiveSheet.Rows("25:75").Hidden = False
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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