How to Hide Columns in Microsoft Excel Based on Cell Value

Hiding columns in Microsoft Excel is a standard procedure for cleaning up reports and dashboards. Typically, users right-click a column header and select “Hide” manually. However, if you are managing dynamic data sets—such as monthly financial reports where you want to hide columns containing zero values or specific text flags—doing this manually is incredibly tedious. While Excel does not have a native “auto-hide column” formula, you can achieve this exact functionality using a very simple piece of Visual Basic for Applications (VBA) code.

Why Use VBA to Hide Columns?

Formulas in Excel (like IF or VLOOKUP) can only change the contents or formatting of a cell; they cannot alter the physical structure of the worksheet itself, such as hiding a column. VBA bridges this gap by allowing you to write a script that scans specific cells and dynamically changes the column’s visibility property based on the data it finds.

Step 1: Access the VBA Editor

To write the script, you need to open the Developer tools.

  1. Ensure the Developer tab is enabled on your Excel ribbon. (If it is not, right-click the ribbon, select “Customize the Ribbon”, and check the “Developer” box).
  2. Click the Developer tab and select Visual Basic (or press Alt + F11).
  3. In the Project Explorer pane on the left side of the VBA window, find the specific worksheet where your data lives (e.g., `Sheet1`).
  4. Double-click that worksheet name to open its dedicated code window.

Step 2: Add the Auto-Hide Code

We will use a `Worksheet_Change` event, which means the code will run automatically every time a value in the sheet is updated.

Copy and paste the following code into the blank window:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    ' Change "A1:Z1" to the row containing your trigger values
    For Each cell In Range("A1:Z1")
        If cell.Value = "Hide" Then
            cell.EntireColumn.Hidden = True
        ElseIf cell.Value = "Show" Then
            cell.EntireColumn.Hidden = False
        End If
    Next cell
End Sub

Step 3: Customize and Test

This script looks at row 1 (from A1 to Z1). If any cell in that row contains the exact word “Hide”, Excel will instantly hide the entire column. If the cell says “Show”, the column remains visible.

  1. Change the Range("A1:Z1") in the code to match the specific row you want Excel to monitor. For example, if you want it to check row 5 for zero values, change it to Range("A5:Z5") and change "Hide" to 0.
  2. Close the VBA Editor (click the red X in the top right).
  3. Test the macro on your worksheet. Type “Hide” into cell C1 and press Enter; column C will immediately vanish.

Crucial Reminder: Because this workbook now contains a macro, you must save it as an Excel Macro-Enabled Workbook (*.xlsm). If you save it as a standard `.xlsx` file, all your VBA code will be permanently deleted upon closing.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.