VBA Macro to hide column when cell is = 4

bebo1306

New Member
Joined
Apr 11, 2012
Messages
5
How do I auto hide column when a specific cell is equal to 4; Cell P2 will have either a 4 or a 5, when it is equal to 4, I need column "O" to be hidden.

Thanks!
 
Well there is two ways to give you what you need. The first code i sent you runs upon cell selection change. You can change that to run off of a keyboard shortcut if you want and have a separate code to run off of a different code to unhide when you want via a keyboard shortcut. Or if you just want to have a quick glance, you can keep the first code the same, and just make the unhide portion a keyboard shortcut. but note, that if you change your cell selection again, it will rehide the column if p2 is equal to 4. Make sense? but you could use this code to unhide

option 1: Keep the first code the same and use this to unhide your column via keyboard shortcut.

Sub Unhide()
'Keyboard Shortcut: Ctrl+h
If Range("O2").EntireColumn.Hidden = True Then
Range("O2").EntireColumn.Hidden = False
End If
End Sub

To assign to a keyboard shortcut, you just need to hit Alt+F8, highlight this macro and go to options. You can then select your keyboard shortcut that way.

Option 2:
Change the first code to this and assign that to a keyboard shortcut :

Sub HideColumn()
If Range("P2").Value = "4" Then
Range("O2").EntireColumn.Hidden = True
Else
If Range("P2").Value <> "4" Then
Range("O2").EntireColumn.Hidden = False
Else
Exit Sub
End If
End If
End Sub


Then use this to unhide via a keyboard shortcut as well:

Sub Unhide()
'Keyboard Shortcut: Ctrl+h
If Range("O2").EntireColumn.Hidden = True Then
Range("O2").EntireColumn.Hidden = False
End If
End Sub

These would provide you with quick keyboard ways to activate or deactivate your macros.
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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