Hide column based on cell value

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
I am using the code below to hide a column based on a cell value.
I need to repeat this macro from column C to column AF.
How do I need to modify it to avoid copying this code for each colum?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Range("C5").Value = 0 Then
Columns("C").EntireColumn.Hidden = True
Else
Columns("C").EntireColumn.Hidden = False
End If
End Sub


Thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I guess you need Worksheet_Change instead of Worksheet_SelectionChanged. Correct?

Code:
Private Sub [B][COLOR="Red"]Worksheet_Change[/COLOR][/B](ByVal Target As Range)
    If Range("C5").Value = 0 Then
        Columns("C:AF").EntireColumn.Hidden = True
    Else
        Columns("C:AF").EntireColumn.Hidden = False
    End If
End Sub
 
Upvote 0
Thanks for the answer.
The code works but it hides all the columns in the spreadsheet and not only the columns from C to AF.
Is there a way to limit it only for the range C:AF ?
 
Upvote 0
Or this way...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Columns("C:AF").EntireColumn.Hidden = Range("C5").Value = 0
End Sub
 
Upvote 0
Mmmm! Not really because the column must be hidden if the value in the related column is 0.
I mean...
if C5 = 0 the Column C must be hidden
if D5 = 0 the Column D must be hidden
etc until column AF

Any thought?
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    For i = 3 To 32
        Cells(1, i).EntireColumn.Hidden = Cells(5, i) = 0
    Next
End Sub

Or this... :)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    For i = 3 To 32: Cells(1, i).EntireColumn.Hidden = Cells(5, i) = 0: Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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