Yes—but Excel has no built-in setting that automatically hides a column when a cell becomes blank. In Excel for Microsoft 365 on Windows, you can use a VBA worksheet event to do it: the example below checks B1:Z1 and hides each matching column when its row 1 cell is blank.
What “hide a column if blank” means in Excel
There are two common interpretations of this request. You might want one particular cell to control a different column—for example, hide column C when A1 is empty—or you might want each column to be hidden when a designated cell within that column is empty. The VBA example later in this article uses the second pattern: B1 controls column B, C1 controls column C, and so on through Z1.
It is also important to distinguish hiding a column from removing its contents. A hidden column remains in the workbook. Its values, formulas, and references still exist, and other formulas can still use them. Hiding changes what is displayed; it does not delete data or prevent someone with access to the workbook from unhiding the column.
What Excel can do without VBA
Excel can hide a column manually, but it does not provide a standard conditional-formatting rule that hides a column when a cell is blank. Conditional formatting changes how cells look; it cannot change a column’s hidden state. Excel’s filter tools can hide rows that do not meet a condition, but they do not automatically hide entire worksheet columns based on cell contents.
For a one-time change, select the column or columns, then choose Home > Format > Hide & Unhide > Hide Columns. To show them again, select the columns on either side of the hidden area and choose Home > Format > Hide & Unhide > Unhide Columns. On Windows, Ctrl+0 hides selected columns; on Mac, Command+0 is the corresponding shortcut in Excel, though system or keyboard settings may affect it.
Manual hiding is often the right choice when the layout changes only occasionally. It does not require macros, and it is easy to understand when another person opens the file. It is not automatic: if a cell later becomes blank, Excel will not hide its column unless you repeat the command.
Automatically hide columns with a worksheet event
The following VBA example monitors the cells B1:Z1. When one of those cells changes, it hides the corresponding column if the cell is empty or contains only ordinary spaces. It also checks again after worksheet calculation, so a formula in one of those cells that changes its displayed result can update the column state.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("B1:Z1")) Is Nothing Then Exit Sub
UpdateBlankColumns
End Sub
Private Sub Worksheet_Calculate()
UpdateBlankColumns
End Sub
Private Sub UpdateBlankColumns()
Dim checkCell As Range
' Check each control cell and set its matching column's visibility.
For Each checkCell In Me.Range("B1:Z1").Cells
If IsError(checkCell.Value) Then
Me.Columns(checkCell.Column).Hidden = False
Else
Me.Columns(checkCell.Column).Hidden = _
(Len(Trim$(CStr(checkCell.Value2))) = 0)
End If
Next checkCell
End Sub
For example, if C1 is blank, column C is hidden. If C1 contains “Quarterly total,” column C is shown. An error value is treated as not blank, so the example shows that column rather than hiding it. The check uses the value in the cell, not whether the cell has formatting, a border, or a fill color.
How to add the VBA code
- Save a copy of the workbook before changing its code. If it is not already a macro-enabled workbook, use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm).
- Open the Visual Basic Editor. On Windows, press Alt+F11. On Mac, use Developer > Visual Basic; the keyboard shortcut can vary with the keyboard and function-key settings.
- In the project pane, find the worksheet that contains B1:Z1. Double-click that sheet—not a standard module—and paste the code into its code window.
- Return to Excel and change a cell in B1:Z1, or recalculate the sheet if those cells contain formulas. If Excel asks about macros, enable them only when you trust the workbook and its source.
Worksheet event procedures must be placed in the relevant worksheet’s code module to respond to that sheet. Pasting them into a standard module will not make the events run automatically. The code affects only the worksheet where it is installed, and the example’s range is deliberately limited to columns B through Z.
Change the trigger cell or range
If a single cell should control a different column, adjust the event range and the column assignment. For instance, to use A1 as the trigger for column C, the event should watch A1 and the update routine should set the hidden state of column C based on A1’s value. This is a useful design when a status cell controls whether a related section is relevant.
If you want to check a different row for every column, change B1:Z1 in both places in the code to the matching range, such as B5:Z5. The loop then checks row 5 while hiding or showing the corresponding columns. Make sure the event’s monitored range and the loop’s checked range agree; otherwise a change might not trigger an update, or the code could evaluate a different set of cells than you intended.
For a large range or a workbook with many sheets, avoid expanding the check to every column unless that is genuinely needed. Each calculation can run the checking routine, so a short, specific range is easier to maintain and can reduce unnecessary work.
What counts as a blank cell?
A genuinely empty cell is blank to this test. A formula can also display nothing while still being present in the cell; if its calculated value is an empty string, the example treats it as blank. This is often what people mean when they ask to hide columns for cells that look empty.
Whitespace can cause surprises. The code uses VBA’s Trim function, which removes ordinary spaces at the beginning and end of text. It therefore treats a cell containing only ordinary spaces as blank. It does not remove every invisible character: a tab or a nonbreaking space may remain and be treated as content. If data comes from a website or another system, a cell that looks empty may contain one of these characters. Clean or inspect the source data if the column does not hide as expected.
Formatting alone does not make a cell nonblank, and a zero is not blank. A cell displaying 0 will keep its column visible. If your rule should treat zero, a dash, or a specific text label as blank, that is a different condition and the VBA test needs to be changed deliberately.
Formula changes, recalculation, and limitations
A worksheet formula cannot directly hide or unhide a column. Formulas return values to cells; they do not control worksheet layout. VBA is what connects the cell result to the column’s Hidden property in this example. The Worksheet_Calculate event is included because a formula’s result can change when its inputs change, even though the formula cell itself was not edited.
Automatic updates depend on macros being allowed to run. If macros are disabled, the event code will not execute, and columns will not respond to edits or recalculation. In manual calculation mode, formula results may not update until Excel recalculates; use Formulas > Calculation Options to check the workbook’s calculation setting. This code also cannot react to changes made in another workbook or external source until the relevant worksheet recalculates.
The event code controls the state of every column in its specified range. If a user manually unhides a column whose control cell is still blank, the next change or recalculation can hide it again. Likewise, if a column in B:Z was hidden for another reason, the code may show it when its control cell is nonblank. Use this approach only when the macro is meant to own the visibility of those columns.
Alternatives and practical choices
- Manual hiding: Best for a one-off presentation or a layout that changes rarely. It works without VBA, but does not respond to cell changes.
- VBA event code: Best when the workbook should react immediately to edits or recalculation. It requires a macro-enabled file and an appropriate macro-security setting.
- Filters: Best when the real goal is to show only records with data. Filters hide rows based on criteria; they are usually more suitable for lists than hiding worksheet columns.
- Grouping: Best when users should be able to expand and collapse a set of columns themselves. Use Data > Group after selecting the columns. Grouping is a manual outline control, not a rule that checks whether cells are blank.
- A separate report or view: Best when different audiences need different fields. A designed report can omit irrelevant fields without repeatedly changing the source sheet’s column visibility.
For a shared workbook, consider whether hiding columns could confuse readers or conceal information they need. A clear label, an instruction note, or a separate presentation sheet may make the workbook easier to use than automatic hiding. Hidden columns are not a security feature; do not rely on them to protect confidential data.
Common problems and safe handling
If nothing happens, first confirm that the code is in the correct worksheet module, the monitored cells are within the stated range, and macros are enabled for a trusted file. Check that the workbook is saved as an .xlsm file. If the trigger cells contain formulas, force a recalculation and confirm that the displayed result is genuinely empty rather than a space, zero, or error.
If columns keep disappearing after someone shows them, that is expected: the code re-applies the cell-based rule on a later edit or calculation. If that behavior is undesirable, use manual hiding or change the code so it runs only when explicitly requested. Before running or adapting VBA, keep a backup. Macro actions generally cannot be reversed with Ctrl+Z, and a mistake in the range could hide more columns than intended.
Frequently Asked Questions
Can conditional formatting hide a column when a cell is blank?
No. Conditional formatting can change a cell’s appearance, but it cannot hide a worksheet column. Use manual hiding, VBA, or a different layout if automatic column visibility is essential.
Will a formula that displays a blank hide the column?
Yes, with the sample event code, provided the formula’s calculated value is an empty string and the worksheet recalculates. Worksheet_Calculate is included to check formula results after recalculation.
Can I hide a column based on a cell in another column?
Yes. Set the code to monitor the trigger cell you want and assign the resulting Hidden state to the target column. The sample maps each control cell in row 1 to its own column, but the trigger and target do not have to match.
Does hiding a column delete its data?
No. The values and formulas remain in the workbook, and formulas can still refer to them. Hiding is only a display choice, not deletion or data protection.
Can this work without enabling macros?
Not as an automatic response to a blank cell using this VBA method. Without macros, hide columns manually, use filters to control row visibility, or build a separate view that excludes fields you do not want shown.
Related
- Excel Shortcut to Select a Column on Windows and Mac
- Excel Shortcut to Select a Row
- Excel Shortcut to Select Visible Cells in a Filtered Range
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel IFERROR With IF: How to Handle Formula ErrorsIn Excel, use IFERROR to return a chosen result when a calculation produces an error, and use IF…
- How to Find the Mode in Excel: Formulas and ExamplesTo find the most frequently occurring number in Excel, enter =MODE.SNGL(A2:A11) in an empty cell, replacing A2:A11 with…
- How to Fill A to Z in Excel With Formulas or AutofillTo fill A to Z in Excel, enter =CHAR(ROWS($A$1:A1)+64) in the first cell, then fill it down 26…
- Excel Formula for Hours Worked: Calculate Time and PayFor a shift that starts in A2 and ends in B2, enter =MOD(B2-A2,1)*24 to return hours as a…
- Excel Unique Dropdown List: Create One That UpdatesTo create a unique dropdown list in Excel for Microsoft 365, generate a deduplicated list with UNIQUE, then…
- SUMIF Criteria Examples: Text, Dates, Numbers, and MoreIn Excel, SUMIF adds values that match one condition; for example, with categories in A2:A7 and amounts in…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.