VBA Code - Hiding rows

Kritty

New Member
Joined
Feb 18, 2019
Messages
4
Hey,

I'm new to VBA and am trying to implement some code into my spreadsheets. At the moment I am trying to hide rows based on a value in a specific cell (K21).

Basically what I am trying to achieve is the following
If cell K21 = 1, hide rows 36 to 46
If cell K21 = 2, hide rows 40 to 46
If cell K21 = 3, hide rows 44 to 46
If cell K21 = 4, hide rows 47 to 47

At the moment my code looks like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$K$21" Then
If Target.Value = 1 Then Rows("36:46").EntireRow.Hidden = True
If Target.Value = 2 Then Rows("40:46").EntireRow.Hidden = True
If Target.Value = 3 Then Rows("44:46").EntireRow.Hidden = True
If Target.Value = 4 Then Rows("47:47").EntireRow.Hidden = True
End If
End Sub


The above code works only if I set the value of cell K21 once. If I try and change the value (lets say from 2 to 3) it will not hide the correct rows. I think it has something to do with my ranges overlapping?

Any help would be much appreciated :)

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Maybe like this.....rows need to be Unhidden before each run !!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$K$21" Then
Cells.EntireRow.Hidden = False
If Target.Value = 1 Then Rows("36:46").EntireRow.Hidden = True
If Target.Value = 2 Then Rows("40:46").EntireRow.Hidden = True
If Target.Value = 3 Then Rows("44:46").EntireRow.Hidden = True
If Target.Value = 4 Then Rows("47:47").EntireRow.Hidden = True
End If
End Sub
 
Upvote 0
Maybe like this.....rows need to be Unhidden before each run !!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$K$21" Then
Cells.EntireRow.Hidden = False
If Target.Value = 1 Then Rows("36:46").EntireRow.Hidden = True
If Target.Value = 2 Then Rows("40:46").EntireRow.Hidden = True
If Target.Value = 3 Then Rows("44:46").EntireRow.Hidden = True
If Target.Value = 4 Then Rows("47:47").EntireRow.Hidden = True
End If
End Sub

Thanks for that! Just tried it and it works perfectly :)
 
Upvote 0
glad to help and thx for the feedback...:beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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