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!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Sub hidecolumn()
If Range("P2").Value = "4" Then
Range("P2").EntireColumn.Hidden = True
Else
End If
End Sub
 
Upvote 0
Try this. Adapted from some code by Mr Andrew Poulsom

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address <> "$P$2" Then Exit Sub
    With Worksheets("Sheet1")
        .Columns("O").Hidden = Target.Value = 4
    End With
End Sub

The code needs to go in the worksheet module
1) Copy the code that you want to use
2) Select the worksheet in which you the code to run
3) Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
4) Where the cursor is flashing, choose Edit | Paste
 
Last edited:
Upvote 0
I changed the column to "O", and that worked...But, when I change cell P2 to "5" how do I get column "O" to automatically unhide?

Thanks!
 
Upvote 0
Try this

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
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


Note: this does have to be put into the worksheet module you want this to run on.
 
Last edited:
Upvote 0
No problem. I can usually help on the small issues. Its the big ones that i sometimes need to defer to some of the other more knowledgeable users on here.
 
Upvote 0
Yes...It's just ugly and in the way when not being used during a regular "4 week" month...I will just manually hide it - I have also used the If Then formula to return blanks when not being used - this helps clean it up a bit.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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