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 […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
This article demonstrates macros that automatically changes the chart bar colors based on the corresponding cell, the first example is […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
The calendar shown in the image above highlights events based on frequency. It is made only with a few conditional […]
Fatou asks: Going back to my question, I had created a table and used the data to create a chart. […]
You can easily change data labels in a chart. Select a single data label and enter a reference to a […]
The image above shows a chart populated with data from an Excel defined Table. The worksheet contains event code that […]
Today I am going to show you how to create a dynamic Gantt chart in excel 2007. A Gantt chart helps […]
The picture above shows a chart that has custom data labels, they are linked to specific cell values. This means […]
I will in this article demonstrate how to set up two drop down lists linked to an Excel chart, the […]
This article describes how to create a map in Excel, the map is an x y scatter chart with an […]
This article demonstrates how to insert pictures to a chart axis, the picture above shows a column chart with country […]
I made a heat map calendar a few months ago and it inspired me to write this article. The heat […]
This article demonstrates a macro that changes y-axis range programmatically, this can be useful if you are working with stock […]
This article demonstrates how to highlight a bar in a chart, it allows you to quickly bring attention to a […]
This article demonstrates how to use drop down lists combined with an Excel defined Table and a chart. This allows […]
This article demonstrates how to highlight a line in a chart based on the selected item in a drop-down list. […]
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 […]
This article demonstrates how to populate a drop down list with filtered values from an Excel defined Table. The animated […]
This article demonstrates how to filter records occurring in only one out of two Excel defined tables. It also shows […]
The image above demonstrates a conditional formatting formula applied to an Excel Table containing random data. The Excel Table has […]
The image above demonstrates a macro linked to a button. Press with left mouse button on the button and the […]
This article demonstrates how to filter duplicate records using a simple formula and an Excel defined table.
I will in this article demonstrate a macro that copies criteria from one Excel Table and applies them to another […]
In this tutorial, I am going to demonstrate how to filter an Excel define Table through a VBA macro. How it […]
This article explains how to filter a data set based on extremely many conditions in an Excel defined Table, in […]
This blog post demonstrates how to filter unique distinct values from an Excel table dynamically. When you change or add […]
Today I am going to demonstrate how amazing pivot tables are! Take a look at this time sheet. You can […]
I will in this article demonstrate three different techniques to sort a data set in Excel. The first method sorts […]
This article demonstrates how to insert and use a scroll bar (Form Control) in Excel. It allows the user to […]
An Excel table allows you to easily sort, filter and sum values in a data set where values are related.
This article demonstrates a macro that automatically applies a filter to an Excel defined Table based on the result from […]
The filter feature in Excel won't allow you to do OR logic between columns, however, you can if you allow […]
This article explains how to calculate the largest and smallest number based on a condition which is if the number […]
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 […]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
This article demonstrates how to run a VBA macro using a Drop Down list. The Drop Down list contains two […]
This workbook contains two worksheets, one worksheet shows a calendar and the other worksheet is used to store events. The […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
In this blog article, I will demonstrate basic file copying techniques using VBA (Visual Basic for Applications). I will also […]
Excel does not resize columns as you type by default as the image above demonstrates. You can easily resize all […]
This article describes how to create an interactive chart, the user may press with left mouse button on a button […]
Rahul asks: I want to know how to create a vlookup sheet, and when we enter a name in a […]
This article describes how to create a button and place it on an Excel worksheet then assign a macro to […]
Question: hi all, thanks for the great formula/array formula. it works great. lately, i noticed that the array formula will […]
This article demonstrates a macro that inserts new worksheets based on names in a cell range. The cell range may […]
In this article, I am going to demonstrate a simple workbook where you can create or delete projects and add […]
Today I would like to share with you these small event handler procedures that make it easier for you to […]
This article explains how to hide a specific image in Excel using a shape as a button. If the user […]
This article describes different ways to locate literal or hardcoded values in formulas. The image above shows the result from […]
This article demonstrates macros that save worksheets to a single pdf file. What's on this webpage Export all worksheets in […]
How to use Excel Tables
Excel categories
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!