Hide and Unhide Rows or Columns Using VBA

Before we send out by email or print a particular Excel workbook we have been working on, we may want to hide specific rows or columns. We do this to prevent the display of specific data to unauthorized people.

In other situations, before we audit a spreadsheet we may have received from a colleague, we may want to unhide all columns and rows to ensure we get a total view of the spreadsheet’s contents.

This tutorial shows you several techniques for hiding and unhiding rows and columns using Excel VBA.

How to Hide Columns Using Excel VBA

Method #1: Excel VBA Procedure to Hide Columns

Suppose we have the following Excel dataset on Sheet1 of the workbook.

We want to use Excel VBA to hide columns E, F, and G, which contain employee data before we print the spreadsheet or send it out by email.

We use the following steps:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
Graphical user interface, application

Description automatically generated
  1. In the Visual Basic Editor, click Insert >> Module to create a new module.
Graphical user interface, application

Description automatically generated
  1. Type the following procedure in the module.
  1. Save the procedure and save the workbook as a Macro-Enabled Workbook. This ensures that the procedure is retained in the workbook for future use.
  2. Click inside the procedure and press F5 to run the code.
  3. Click the View Microsoft Excel button on the toolbar.

You are taken back to the active worksheet containing the dataset. Notice that columns E, F, and G are hidden.

Note: If you want to hide several non-contiguous columns, separate the column references by commas. For example, to hide columns E and G, we use the following sub-procedure:

Method #2: Excel VBA to Hide Columns Based on Cell Values

Suppose we have the following Excel dataset on Sheet2 of the workbook.

We want to use Excel VBA to hide the columns that have the word “Hide” in cells B29 and C29.

We use the steps below:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Editor, click Insert >> Module to create a new module.
  3. Type the following procedure in the module:
  1. Place the cursor anywhere in the procedure and press F5 to run the code.
  2. Click the View Microsoft Excel button on the toolbar to switch to the active workbook that contains the dataset. Notice that columns B and C that have the word “Hide” in cells B29 and B30 respectively are hidden.

How to Hide Rows Using Excel VBA

Method #1: Excel VBA Procedure to Hide Rows

Suppose we have the following Excel dataset on Sheet1 of the workbook.

We want to hide rows 6, 7, 8, and 9, which contain data on the orphanage income before we print the spreadsheet or send it out by email.

We use the following steps:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Editor, click Insert >> Module to create a new module.
  3. Type the following procedure in the module:
  1. Click inside the procedure and press F5 to run the code.
  2. Click the View Microsoft Excel button on the toolbar. You are taken back to the active worksheet. Notice that rows 6, 7, 8, and 9 are hidden.

Note: If you want to hide several non-contiguous rows, separate the row references by commas. For example, to hide rows 6 and 8, we use the following sub-procedure:

Method #2: Excel VBA to Hide Rows Based on Cell Values

Suppose we have the following Excel dataset on Sheet3 of the workbook.

We want to use Excel VBA to hide all the rows that have the word “Hide” in column D.

We use the steps below:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Editor, click Insert >> Module to create a new module.
  3. Type the following procedure in the module:
  1. Place the cursor anywhere in the procedure and press F5 to run the code.
  2. Click the View Microsoft Excel button on the toolbar to switch back to the active worksheet containing the dataset. Notice that rows 6, 7, 8, and 9 that have the word “Hide” in cells D6, D7, D8, and D9, respectively, are hidden.

How to Unhide Columns Using Excel VBA

Suppose we have the following dataset on Sheet4 whose columns B and C are hidden.

We want to unhide the two columns using an Excel VBA sub-procedure.

We use the following steps:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Editor, click Insert >> Module to create a new module.
  3. Type the following procedure in the module:
  1. Click inside the sub-procedure and press F5 to run the code.
  2. Click View Microsoft Excel on the toolbar to switch back to the active worksheet containing the dataset. Notice that columns B and C are unhidden.

How to Unhide Rows Using Excel VBA

Suppose we have the following dataset whose rows 6, 7, 8, and 9 are hidden. The dataset is on Sheet5.

We want to unhide the four rows using Excel VBA.

We use the following steps:

  1. Click Developer >> Code >> Visual Basic to open the Visual Basic Editor.
  2. In the Visual Basic Editor, click Insert >> Module to create a new module.
  3. Type the following procedure in the module:
  1. Click inside the sub-procedure and press F5 to run the code.
  2. Click View Microsoft Excel on the toolbar to switch back to the active worksheet containing the dataset. Notice that rows 6, 7, 8, and 9 are unhidden.

Conclusion

This tutorial has looked at how to hide or unhide columns and rows using Excel VBA. We hope you found the information helpful.

Tomasz Decker is an Excel specialist, skilled in data analysis and financial modeling.

Posted in vba