Welcome to the MrExcel board!Peter,
This macro works perfectly and is fantastic! I am new to macros and do have a question about modifying your macro. I have the need to be able to select multiple columns based on different values. IE: column headers equal to 12,17, 37 and 142. Any help will be greatly appreciated!
Randy
So are "12,17 etc" column numbers (that is columns L, Q, etc) or are they headings (in row 1)?Sure Peter. What I mean is that I need a way to input multiple values in say cell B1 or multiple cells and then have the corresponding columns visible with all others hidden. Hope that makes more sense.
Rany
Excel Workbook | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | K | L | |||
1 | Visible Headers | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | Header 6 | Header 7 | Header 8 | Header 9 | |||
2 | data | data | data | data | data | data | data | data | data | ||||
3 | data | data | data | data | data | data | data | data | data | ||||
4 | data | data | data | data | data | data | data | data | data | ||||
5 | data | data | data | data | data | data | data | data | data | ||||
6 | data | data | data | data | data | data | data | data | data | ||||
7 | |||||||||||||
Show or Hide Columns |
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Headers As Range, cel As Range, VisibleList As Range
If Not Intersect(Target, Columns("B")) Is Nothing Then
Application.ScreenUpdating = False
Set Headers = Range("C1:K1")
Set VisibleList = Range("B1", Range("B" & Rows.Count).End(xlUp))
If VisibleList.Cells.Count = 1 Then
Headers.EntireColumn.Hidden = False
Else
For Each cel In Headers
cel.EntireColumn.Hidden = IsError(Application.Match(cel.Value, VisibleList, False))
Next cel
Application.ScreenUpdating = True
End If
End If
End Sub
Excel Workbook | |||||||||
---|---|---|---|---|---|---|---|---|---|
B | D | E | G | I | K | L | |||
1 | Visible Headers | Header 2 | Header 3 | Header 5 | Header 7 | Header 9 | |||
2 | Header 7 | data | data | data | data | data | |||
3 | Header 5 | data | data | data | data | data | |||
4 | data | data | data | data | data | ||||
5 | Header 2 | data | data | data | data | data | |||
6 | Header 9 | data | data | data | data | data | |||
7 | Header 3 | ||||||||
8 | |||||||||
Show or Hide Columns |
Try this:The only thing I have left to figure out is how to make the column headers a selectable list.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range, VisibleList As Range
If Not Intersect(Target, Columns("B")) Is Nothing Then
Application.ScreenUpdating = False
Set VisibleList = Range("B1", Range("B" & Rows.Count).End(xlUp))
If VisibleList.Cells.Count = 1 Then
Range("Headers").EntireColumn.Hidden = False
Else
For Each cel In Range("Headers")
cel.EntireColumn.Hidden = IsError(Application.Match(cel.Value, VisibleList, False))
Next cel
Application.ScreenUpdating = True
End If
End If
End Sub