VBA Hide Columns

Updated on January 1, 2024
Article byJeevan A Y
Edited byAshish Kumar Srivastav
Reviewed byDheeraj Vaidya, CFA, FRM

Excel VBA Hide Columns

Hiding is simple, but you need to understand the concept here. To hide the column using VBA, we need to determine which column we need to hide. To specify the column to be hidden, we need to use the RANGE objectUse The RANGE ObjectRange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more.

Range("A:A")

Then, we need to use the Property “Entire Column.”

Range("A:A").EntireColumn

We need to use the “Hidden” property in the column property.

Range("A:A").EntireColumn.Hidden

Then, we need to set the Hidden property to TRUE.

Range("A:A").EntireColumn.Hidden = TRUE

It will hide column A.

How to Hide Columns in VBA?

We can hide columns in several ways. We can hide a column using a Range object using the CELLS propertyUsing CELLS PropertyCells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel cells.read more. We need to construct our VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more based on the method we use.

–>> If you want to learn Excel VBA professionally, then our VBA Basic Course (16+ hours) is the perfect solution. In our Basic Excel VBA course you learn the skill of automating tasks using Variables, Data Types, Ranges, and Cells in VBA. Master Control Structures, including Conditional Statements and Loops, and discover techniques for manipulating data through sorting, filtering, and formatting. By the end of the course, you will be able to apply your acquired skills to real projects, culminating in an Excel VBA project which will solidify your ability to create efficient, automated solutions.

You can download this VBA Hide Columns Excel Template here – VBA Hide Columns Excel Template

Example #1 – Hide using Range Object

If you want to hide a particular column, then specify the column inside the RANGE object. For example, look at the below data.

vba hide column example 1.1

Now, we will hide the entire column A using the following code.

Code:

Sub Range_Hide()

    Range("A:A").EntireColumn.Hidden = True

End Sub
vba hide column example 1.2

So, when we run the code, we will get the following result.

vba hide 1

Example #2 – Hide using Columns Property

If you want to hide the column using the Columns property, then the below code is for you.

Code:

Sub Range_Hide()

    Columns("B").Hidden = True

End Sub
vba hide column example 2.1

It will hide column B. Here, you must be thinking about where the Entire Column property is. When you use the Columns property, it is unnecessary to use the Entire Column property.

We can still use the Entire Column property but not necessarily required. And code for the same is given below.

Code:

Sub Columns_Hide()

    Columns("B").EntireColumn.Hidden = True

End Sub
vba hide column example 2.2

It also should work fine.

We can use the column number as well instead of an alphabetic reference. And code for the same is given below.

Code:

Sub Columns_Hide()

    Columns(4).EntireColumn.Hidden = True

End Sub
vba hide column example 2.3

It will hide Column D.

vba hide 2

Example #3 – Hide Multiple Columns

We can hide multiple columns at a time as well. We need to mention the first and the last column so that in between columns also will be hidden. Using the following code to hide multiple columns for the same data as example #1.

Code:

Sub Columns_Hide()

    Range("A:C").EntireColumn.Hidden = True

End Sub
vba hide column example 3.1

It will hide column A to C.

We can also use the following code to hide multiple columns in Excel VBA.

Code:

Sub Multi_Columns_Hide()

    Columns("A:C").EntireColumn.Hidden = True

End Sub
vba hide column example 3.2

The above methods will hide the first three columns: A, B, and C.

vba hide column example 3.3

Example #4 – Hide Columns with Single Cell

We can also hide a column based on a single cell referenceCell ReferenceCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1.read more. We do not need to give the full column reference to hide the column. With just a single cell reference, we should be able to hide a column.

Code:

Sub Single_Hide()

    Range("A5").EntireColumn.Hidden = True

End Sub
vba hide column example 5.3

It will hide the entire column A.

vba hide column example 4.2

Example #5 – Hide Every Alternative Column

Assume you have data, something like the below image.

vba hide column example 5.1

We need to hide every alternative column, which is blank. We need to use loops; the below code will do the job for us.

Code:

Sub AlternativeColumn_Hide()

    Dim k As Integer

    For k = 1 To 7
        Cells(1, k + 1).EntireColumn.Hidden = True
        k = k + 1
    Next k

End Sub
example 5.2

It will hide every alternate column.

5 - GIF

Example #6 – Hide Every Empty Column

In the previous example, every other alternative column was blank. So we have hidden it easily. But look at the below data.

example 6.1

Here, the empty columns pattern is not standard. In these cases, the below code will hide all the empty columns. It does not matter what the pattern is.

Code:

Sub Column_Hide1()

    Dim k As Integer

    For k = 1 To 11
       If Cells(1, k).Value = "" Then
             Columns(k).Hidden = True
       End If
    Next k

End Sub
example 6.2

When you run the code, you will get the result as follows.

example 6.3

Example #7 – Hide Columns Based On Cell Value

Now we will see how to hide columns based on the cell value. For example, look at the below data.

example 7.1

Here, we want to hide all the columns if the heading is “No.” The below code will do it for me.

Code:

Sub Column_Hide_Cell_Value()

    Dim k As Integer

    For k = 1 To 7
        If Cells(1, k).Value = "No" Then
           Columns(k).Hidden = True
        End If
    Next k

End Sub
example 7.2

When you run the code, you will get the result as follows.

7 - GIF

Recommended Articles

This article has been a guide to VBA Hide Columns. Here, we discuss hiding columns in VBA Excel and the practical examples and downloadable Excel sheets. You can learn more about VBA from the following articles: –