Hi all,
I have a piece of VBA code which hides and shows specific columns within a range based on the value of a particular cell.
I have attached an example spreadsheet to demonstrate this, i.e. the variable cell being B2. If you type "A" into this cell, columns that have "A" in row 2 will only show. The same is true if you type "B".
My range is currently looking at D2:H2 however I wish to omit column F from the range. I'm new to VBA hence not sure what the correct syntax is for omitting columns.
The code i have used for Sheet 1 is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range, Headers As Range
Dim s As String
Set Headers = Worksheets("Sheet1").Range("D2:H2")
If Target.Address = "$B$2" Then
s = Range("$B$2")
Application.ScreenUpdating = False
If s = "" Then
Headers.EntireColumn.Hidden = False
Else
For Each cel In Headers
cel.EntireColumn.Hidden = Not cel.Value = s
Next cel
End If
Application.ScreenUpdating = True
End If
End Sub
Thank you in advance, any feedback much appreciated.
Bookmarks