Row Hide

psamu

Active Member
Joined
Jan 3, 2007
Messages
462
If I want to hide row a15 when cell value in k5=1 and also if cell value in k5=2 then hide row A16. Please help me with the vba code.

Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi Psamu
You say you want to hide row "A15" when K5 =1, do you mean the entire row "A" or cell A15.

Regards
Michael M
 
Upvote 0
Thanks. Yes i mean entire row "A15" when K5 =1, and When K5=2 then enire row A16 need to be hidden.
 
Upvote 0
Thanks. Yes i mean entire row "A15" when K5 =1, and When K5=2 then enire row A16 need to be hidden.

and also if I want to hide A15 and A19 when value in K5=1 and When value in K5=2 Row A16 and A20 hidden. Thanks.
 
Upvote 0
What if k5=1 AND k6=2 at the same time.....does that happen ?

Or are they going to be independant of each other

Regards
Michael M
 
Upvote 0
NO. Chances will be less, but if that happens then what will happen?
 
Last edited:
Upvote 0
psamu
This will do what you require, but I'm sure if you wait a little while there will be a number of examples of more elegant code posted to do the same thing.

Code:
Sub hidden()
    If Range("K5").Value = 1 Then
        Rows("15").hidden = True
        Rows("19").hidden = True
    End If
    If Range("K6").Value = 2 Then
        Rows("16").hidden = True
        Rows("20").hidden = True
    End If
End Sub

Regards
Michael M
 
Upvote 0
If you would like them to go back to being visible when they are not those values when you run the macro:

Code:
Sub test()
Rows(15).Hidden = Range("K5") = 1
Rows(19).Hidden = Range("K5") = 1
Rows(16).Hidden = Range("K6") = 2
Rows(20).Hidden = Range("K6") = 2
End Sub

If you'd like it to work automatically when you change the values, you can use a Worksheet Change event:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim d As Range
Set d = Intersect(Target, Range("K5:K6"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
    Rows(15).Hidden = Range("K5") = 1
    Rows(19).Hidden = Range("K5") = 1
    Rows(16).Hidden = Range("K6") = 2
    Rows(20).Hidden = Range("K6") = 2
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
Thanks. It is working, but I need to run the macro every time there is a change. also If the value in K5=1 and when K5 changes to 2 then what is the code. Thanks.
 
Upvote 0
I gave it to you. It is the worksheet change code.

Take the code you have out of the module.

Copy the Worksheet Change code I posted.

Right click on the tab for the sheet you want this to happen on.
Click on View Code
Paste into white area.
Hit Alt-q

The code will be saved with the workbook when you save the workbook.
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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