EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login

VBA Hide Columns

By Ashwani JaiswalAshwani Jaiswal

Home » VBA » Blog » VBA Lookup & Reference Functions » VBA Hide Columns

VBA Hide Columns

Excel VBA Hide Columns

Hiding and unhiding columns in excel is the simplest task to do. And automating this process is even easy to do. VBA Hide Column does not need huge code to write. But before we move ahead in writing the code for hiding the column in VBA, we need to understand a few things and requirement. We need to know the column name or sequence which we want to hide and the range of the cell which is an optional thing.

How to Hide Columns in Excel VBA?

We will learn how to hide a column in VBA with a few examples in Excel.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Excel VBA Hide Column – Example #1

For this, we need data in the form of multiple filled columns. Here we have a data set where we have randomly taken the numbers starting from 1 to 40 in different 4 columns A, B, C and D respectively and all columns are colored in different colors just to have a better visual on the webpage.

VBA Hide Columns Example 1-1

Suppose, if we want to hide Column A and B then first we need to select both the columns as shown below.

VBA Hide Columns Example 1-2

Now, right-click and select Hide option as shown below.

Select Hide option

The selected columns will get hidden after that. We could only see column C and D with data.

VBA Hide Columns Example 1-4

Now if similar work can be done by VBA macro as well. For this, we would need a module. So go to VBA and select a Module from Insert menu list as shown below.

Insert Module

In the newly opened Module, write the subcategory of VBA Hide Column or we can choose any other name as per our choice as shown below.

Code:

Sub Hide_Column()

End Sub

VBA Hide Columns Example 1-6

First, select the column/s that we need to hide. Here we are hiding column C.

Code:

Sub Hide_Column()

Range("C:C")

End Sub

VBA Hide Columns Example 1-7

As we want to hide entire Column C, select the EntireColumn function followed by Hidden as shown below.

Code:

Sub Hide_Column()

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

End Sub

EntireColumn function

Now, to make this happen, we can choose answer TRUE or FALSE. If we choose Hidden as TRUE then, it would as hiding function or if we choose FALSE then it would work as unhiding function. As we want to hide the selected columns C from the range, so we would select TRUE here.

Code:

Sub Hide_Column()

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

End Sub

VBA Hide Columns Example 1-9

Now compile the code and run it by clicking on the Play button located below the menu bar in VBA. We will see, column C is now hidden, now it only shows columns A, B, and D.

VBA Hide Columns Example 1-10

If we want to hide more than one column together. Then in the range, we need to select those columns as shown below. Here, we are selecting columns B and C.

Code:

Sub Hide_Column()

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

End Sub

VBA Hide Columns Example 1-11

Now again run the code.

VBA Hide Columns Example 1-12

Here we can see, column B and C are hidden now. And only column A and D are visible.

Excel VBA Hide Column – Example #2

Instead of selecting the columns by mentioning the name, we can hide any column by their sequence as well. This process many times don’t get preferred because we can easily get to know the name of the column at which alphabetical location the column is located. This process is similar to the code which we have seen in example #1, where we have selected the column name as B and B:C, we will select the sequence number at which the column is located. If suppose we want to hide the column A using the code of example-1, we just need to place the sequence number instead of selecting it with the help of Range.

Code:

Sub Hide_Column2()

Columns(1).EntireColumn.Hidden = True

End Sub

VBA Hide Columns Example 2-1

Now, run this code by pressing the F5 key or by clicking on the Play button. We will see, column A which is at the first position is now hidden.

VBA Hide Columns Example 2-2

Excel VBA Hide Column – Example #3

There is another way to hide the column using VBA code. This is also as easy as the code which we have seen in example #1. For this again we will use the same data that we used in the above example. Write the subcategory in any name or in the name of a performed function.

Code:

Sub Hide_Column3()

End Sub

VBA Hide Columns Example 3-1

Select any range of column that we need to hide. Here, we are selecting the same column C.

Code:

Sub Hide_Column3()

Range("C:C")

End Sub

VBA Hide Columns Example 3-2

To activate the selection of required column, select Columns function as shown below.

Code:

Sub Hide_Column3()

Range("C:C").Columns

End Sub

Select Columns function

Then use Hidden command, to activate hide and unhide function in VBA.

Code:

Sub Hide_Column3()

Range("C:C").Columns.Hidden

End Sub

Use Hidden command

And select TRUE for hiding the selected column and FALSE for unhiding the selected hidden column.

Code:

Sub Hide_Column3()

Range("C:C").Columns.Hidden = True

End Sub

VBA Hide Columns Example 3-5

Column C which was our range column is now hidden.

VBA Hide Columns Example 3-6

Excel VBA Hide Column – Example #4

There is another simplest way to automate the process of hiding columns in VBA. For this again write the subcategory in the name of VBA Hide Columns.

Code:

Sub Hide_Column4()

End Sub

VBA Hide Columns Example 4-1

Directly select the column that we need to hide. Here again, we are selecting the same column C.

Code:

Sub Hide_Column4()

Columns("C")

End Sub

VBA Hide Columns Example 4-2

And select the Hidden command as TRUE to activate the hiding process. Or select FALSE to activate the unhiding process.

Code:

Sub Hide_Column4()

Columns("C").Hidden = True

End Sub

Command as TRUE

We will see, column C is again hidden.

Pros & Cons of Excel VBA Hide Column

  • The process of VBA Hide Column is very easy to implement.
  • We can hide any type of column Range whether it is in sequence or in random positions.
  • As the process of VBA Hide Column is easy to apply but in manually, it also as easy as doing it by VBA code. So, it does not contribute much in saving. Highly recommended only when we are working on huge code writing.

Things to Remember

  • We hide and unhide the column with the help of some VBA code. For unhiding, use FALSE instead of TRUE for functioning purpose.
  • Column sequence number also works for hiding the column apart from selecting the column name which is mentioned alphabetically.

Recommended Articles

This is a guide to VBA Hide Columns. Here we discuss how to hide columns in Excel using VBA Code along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA Worksheets
  2. VBA UserForm
  3. VBA with
  4. VBA AND

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
VBA Lookup & Reference Functions
  • VBA Lookup & Reference
    • VBA Selecting Range
    • VBA Range
    • VBA Match
    • VBA Transpose
    • VBA Delete Row
    • VBA Range Cells
    • VBA Delete Column
    • VBA Union
    • VBA Insert Column
    • VBA Named Range
    • VBA Hyperlink
    • VBA Last Row
    • VBA Name
    • VBA OFFSET
    • VBA Hide Columns
    • VBA Selection Range
    • VBA Columns
    • VBA Insert Row
    • VBA LOOKUP
    • VBA VLOOKUP Function
    • VBA Resize
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

Download VBA Hide Columns Excel Template

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Excel VBA Bundle (120+ Courses, 500+ hours of Videos) Learn More