Hide specific columns programmatically
This article describes a macro that hides specific columns automatically based on values in two given cells.
I am also going to show you how to hide columns in an Excel Table programmatically. The Excel Table is the data source for a chart, it changes based on which value the user selects using a drop-down list.
Table of Contents
eg:
B1 and B2 hold header names chosen by the user.
range C1 to P2 holds all headers.
Hide ALL columns except those sharing names found in B1 and B2?
VBA code
'Event code is rund when a cell changes Private Sub Worksheet_Change(ByVal Target As Range) 'Dimension variables and declare data types Dim Value As Range, Cells As Range, Cell As Variant Dim c As Integer, b As Boolean 'Check if cell B1 or B2 is changed If Not Intersect(Target, Range("B1:B2")) Is Nothing Then 'Stop screen from refreshing Application.ScreenUpdating = False 'Unhide columns in cell range C:P Worksheets("Cyril").Range("C1:P2").EntireColumn.Hidden = False 'Iterate through 0 (zero) to 13 For c = 0 To 13 'Save range object reference to variable Cells Set Cells = Worksheets("Cyril").Range("C1:C2").Offset(0, c) 'Save False to variable b b = False 'Iterate through each cell in variable Cells For Each Cell In Cells 'Iterate through each value in cell range B1:B2 in worksheet Cyril For Each Value In Worksheets("Cyril").Range("B1:B2") 'If statement checks whether variable Value equals variable Cell If Value = Cell Then 'Save True to variable b b = True End If 'Continue with next value in variable Value Next Value 'Continue with next cell Next Cell 'Check if variable b is equal to False If b = False Then 'Hide column Cells.EntireColumn.Hidden = True End If 'Continue with next number Next c 'Show changes to Excel user Application.ScreenUpdating = True End If End Sub
Animated image
The animated image above shows what happens when the user enters column header names in cell B1 and B2.
Where to put the code?
- Press with right mouse button on on the tab with sheet name "Cyril" located at the bottom of your screen.
- Press with left mouse button on on "View Code", this opens the Visual Basic Editor and takes you to the worksheet module.
- Paste VBA code to worksheet module.
- Return to Excel.
Hiding columns in a table
The image above shows a drop-down list in cell A15 that allows you to control which data series you want to be graphed. When the value in cell A15 changes the following event macro hides all columns in the Excel Table except the chosen name. The chart changes as well and shows only what the Excel Table shows.
I highly recommend you use a slicer if you have Excel 2010 or a later version, there is no need for a macro.
'Event code is rund when a cell is changed Private Sub Worksheet_Change(ByVal Target As Range) 'Dimension variable and declare data type Dim c As Integer 'Check if cell A15 is changed If Not Intersect(Target, Range("A15")) Is Nothing Then 'Stop screen from refreshing Application.ScreenUpdating = False 'Unhide all columns for table Table1 Range("Table1").EntireColumn.Hidden = False 'Iterate from 2 to the number of columns in Table1 For c = 2 To Range("Table1").Columns.Count 'Check if a cell is not equal to the value in cell A15 If Range("Table1[#All]").Cells(c) <> Range("A15") Then 'Hide column based on variable c representing the cell number in Table1 Range("Table1").Cells(c).EntireColumn.Hidden = True End If 'Continue with next number in variable c Next c 'Show changes to user Application.ScreenUpdating = True End If End Sub
Charts category
Question: How do I create a chart that dynamically adds the values, as i type them on the worksheet? Answer: […]
I recently discovered an interesting technique about using a user defined function in a HYPERLINK function. Jordan at the Option […]
This article demonstrates macros that automatically changes the chart bar colors based on the corresponding cell, the first example is […]
Table category
This article demonstrates different ways to reference an Excel defined Table in a drop-down list and Conditional Formatting. There are […]
This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]
This article demonstrates a formula that counts unique distinct values filtered from an Excel defined Table. Debra Dalgleish described in […]
Macro category
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
How to use Excel Tables
5 Responses to “Hide specific columns programmatically”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Thanks.
One complication:
Given the following setup:
A B C A B C A B C
1 1 1 2 2 2 3 3 3
DATA DATA DATA DATA DATA DATA DATA DATA DATA
DATA DATA DATA DATA DATA DATA DATA DATA DATA
That is the first Header Row contains 3 choices, A or B or C
Then the second Header Row contains as well 3 choices 1 or 2 or 3.
Selecting A would display A1 A2 and A3
Selecting 1 would display A1, B1 and C1
Selecting A and 1 would display A1 only
Selecting none that is leaving the cells B1 and B2 blank would display the whole range
Cyril,
Yes, very nice.
works fine on a mac as well.
added button to execute "unhide".
Here is another way to write the Change event code for the Table (it eliminates iterating the columns)...
Rick Rothstein (MVP - Excel),
Thank you for your contribution!