Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Hiding Columns Based on a Cell Value.

Hiding Columns Based on a Cell Value

Written by Allen Wyatt (last updated February 20, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


7

Excel's great conditional formatting capabilities allow you to change the formatting of cells based on the content of a cell. There is no way, unfortunately, to easily hide entire columns of data based on the value of a particular cell.

You can, however, achieve the desired effect by using a macro to analyze the cell and adjust the Hidden attribute of the row you want to conditionally hide. The following simple macro, for instance, examines the contents of cell B4 and, if the cell contains 0, hides column H. If cell B4 does not contain 0, then column H is displayed.

Sub HideColumn1()
    If Range("B4").Value = 0 Then
        Columns("H").EntireColumn.Hidden = True
    Else
        Columns("H").EntireColumn.Hidden = False
    End If
End Sub

If you want the hiding and unhiding of the column to be done in real time, you can use the following version of the macro. Just make sure that you put this version in the code window for the worksheet on which you want it to work.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("B4").Value = 0 Then
        Columns("H").EntireColumn.Hidden = True
    Else
        Columns("H").EntireColumn.Hidden = False
    End If
End Sub

Notice that the guts of the two macros are the same. The only difference is that the second version is triggered by an event within Excel—the changing of which cell is currently selected. This means that every time you move from one cell to another, the value in B4 is checked and column H is either hidden or unhidden.

If it is possible that the contents of cell B4 could be empty, then it is possible that Excel will interpret that emptiness as a zero value. In that case, you can modify the macro just a bit so that it checks for an empty cell.

Sub HideColumn2()
    Dim rCell As Range
    Set rCell = Range("B4")

    Columns("H").EntireColumn.Hidden = False
    If (Not IsEmpty(rCell)) _
      And (IsNumeric(rCell)) _
      And (rCell.Value = 0) Then
        Columns("H").EntireColumn.Hidden = True
    End If
End Sub

This version of the macro actually checks three conditions: that B4 is not empty, that it contains a numeric value, and that the value is 0. If all three of these conditions are met, then column H is hidden.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9730) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Hiding Columns Based on a Cell Value.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing a Draft of a Worksheet

Want to print out the fastest possible copy of your worksheet? You do so by printing a draft, discussed in this tip.

Discover More

Different AutoText Entries in Header and Footer

With lots of AutoText entries defined in your system, you may wonder why you can't see all of the ones you need when ...

Discover More

Maintaining Formatting when Inserting Documents

Word allows you to easily insert the contents of one document into another. Doing so, however, may result in unintended ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Double-Clicking to Widen Columns Won't Work

One way you can widen the columns in a worksheet to fit whatever is in the column is by double-clicking the right edge of ...

Discover More

Shortcuts to Hide Columns

Need a quick way to hide and unhide columns in a worksheet? The shortcuts described in this tip can help fill the bill.

Discover More

Unhiding a Single Column

In a worksheet with lots of hidden columns it is a real pain to try to unhide just one or two columns. The best solution ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2023-02-16 09:55:49

J. Woolley

@Matt
For adjacent columns like H, I, and J, change Columns("H").... to Columns("H:J")....
For non-adjacent columns like H and K, simply add additional statements like Columns("K")....


2023-02-15 15:35:42

Matt

Is there a way to add hide than one column with this Macro?


2021-07-24 10:07:38

abdul rahman

thanks for the tips to hide columns based on a cell value ... However my spreadsheet has the cell value changed from a different worksheet (linked) .. it does not hide the columns automatically until I go to that worksheet and enter something in that spreadsheet (then only, the columns will be hidden) ..

how do I fix this issue?


2021-02-21 11:12:07

JMJ

@Willy
Yeah! Small is beautiful!
Thanks


2021-02-21 10:53:24

J. Woolley

@Willy
A round of applause! I love your one-liners.


2021-02-20 11:39:46

Willy Vanhaelen

To clarify my one-liner:
[H:H] is a shortcut for Column("H")
[B4] is a shortcut for Range("B4")

This one line of code can replace all the code in any of the three macro's in this tip.


2021-02-20 05:33:19

Willy Vanhaelen

Here is a one-liner that hides column H if there is a 0 in B4 and unhides it if it is >=1 or empty:

Private Sub Worksheet_Change(ByVal Target As Range)
[H:H].EntireColumn.Hidden = ([B4] = 0 And [B4] <> "")
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.