You hide columns in Excel VBA by setting the Hidden property of the target columns to True. The shortest working version is Columns("C").Hidden = True or, for a block of columns, Columns("C:E").Hidden = True. That single line goes inside a normal Sub procedure in a standard module, and you can attach it to a button or an event later. This guide walks through the safe way to write it, how to handle ranges on multiple sheets, how to unhide everything again, and the mistakes that make the code fail or hide the wrong thing.
What You'll Need
- A desktop copy of Excel with macros enabled. VBA isn't available in Excel for the web and editing macros on mobile is not practical.
- A workbook saved as .xlsm (Excel Macro-Enabled Workbook) or .xlsb. A plain .xlsx will drop your code.
- The Developer tab visible. If it isn't, turn it on in File > Options > Customize Ribbon and tick Developer on the right-hand list.
- Macro security set to allow the workbook to run, or trust the folder you keep it in.
Step-by-Step
- Open the VBA editor. Press
Alt + F11on Windows orOptions + F11on a Mac. The editor opens in its own window. Note that ribbon and menu labels can differ slightly by version, platform and region, but the editor layout is stable. - Insert a standard module. In the editor choose Insert > Module. A blank code pane appears under the project tree on the left. Code in a module can be run manually or called from a button; code in a sheet module only runs in response to events on that sheet.
- Write the Sub wrapper. Type
Sub HideColumns(), press Enter, and the editor addsEnd Subautomatically. Everything else goes between the two lines. - Always name the worksheet. Rather than relying on whichever sheet happens to be active, point at the sheet explicitly:
Worksheets("Data").Columns("C").Hidden = True. If you leave out the sheet, the code acts on the active sheet, which is the most common cause of "it hid the wrong columns". - Use the Columns property with a string address.
Columns("C")hides one column,Columns("C:E")hides a block, andColumns("A,C,F")hides non-adjacent columns. You can also pass whole numbers:Columns(3).Hidden = Truehides column C, which is useful inside a loop. - Hide a contiguous block from a cell reference instead. If you already know the start and end columns,
Range("C1:E1").EntireColumn.Hidden = Truedoes the same job and reads well when the range comes from a variable.EntireRowis the equivalent for rows. - Test it immediately. Click anywhere inside the Sub and press
F5. If Excel asks which macro to run, pickHideColumns. Switch back to the workbook withAlt + F11to confirm the columns are gone. - Add a guard clause for robustness. If your column numbers are calculated at runtime, wrap them in a check:
If colNum >= 1 And colNum <= Columns.Count Then Columns(colNum).Hidden = True. This stops a bad value throwing a runtime error or hiding something unintended. - Turn screen updating off for bulk work. When hiding many columns in a loop, surround the loop with
Application.ScreenUpdating = Falseand set it back toTrueat the end. It runs faster and avoids visible flicker. Always restore it, even on error — a line that resets it in an error handler is worth the effort. - Wire it to a button. Back on the sheet, go to Developer > Insert > Button (Form Control), draw it, and assign the
HideColumnsmacro when prompted. You can also assign a keyboard shortcut via Developer > Macros > Options. - Write the unhide line. Add a companion Sub with
Columns("C:E").Hidden = False. To clear everything at once useCells.EntireColumn.Hidden = False, which restores every column on the sheet regardless of how it was hidden. - Save as macro-enabled and test after reopening. Close the workbook, reopen it, and run the macro again. If it has vanished, the file was saved in the wrong format or macro security blocked it.
Common Mistakes
- Omitting the sheet reference.
Columns("C").Hidden = Trueacts on the active sheet. If a user clicks a different tab before running it, the columns disappear in the wrong place. The fix takes four extra words. - Hiding a range instead of whole columns.
Range("C1:E10").Hidden = Truehides those rows, not the columns, becauseHiddenon a multi-cell range refers to rows. Add.EntireColumnbefore.Hidden. - Using the wrong letter-to-number mapping. Column A is 1, B is 2, Z is 26, AA is 27. Hard-coding
Columns(26)when you meant Z is fine; assuming the other way round is not. - Saving as .xlsx. Excel warns you and then strips the VBA project on save. Keep the macro workbook as .xlsm.
- Hiding the column your button sits in. If a control is anchored over column C and you hide C, the button disappears too. Move controls to a column you never hide, or to a separate sheet.
- Forgetting that hidden columns still calculate and still appear in unprotected references. Hiding is cosmetic. If the data is sensitive, use sheet protection or a separate sheet rather than hiding.
- Leaving
ScreenUpdating = Falseon an error. The application appears frozen. Reset it in the error handler.
Tips
- Store the column letters or numbers in a constant or a named cell so the code and the sheet layout stay in step when columns get inserted.
- Use
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Data")and referencews.Columnsthroughout.ThisWorkbookpoints at the file containing the code, which is safer thanActiveWorkbook. - If a sheet is protected, hiding columns fails silently or throws an error. Unprotect first with
ws.Unprotect Password:="..."and reprotect afterwards, noting that protection without a password is still meaningful for structure. - Combine hide and unhide in one toggle using
If ws.Columns("C").Hidden Thento decide which branch to run. - Keep a private "Reset" macro that unhides all columns and rows and clears filters, so a broken layout is one click from recovery.
Troubleshooting
Nothing happens when you press F5. Check the cursor is inside the Sub, not in a different procedure. Confirm the module is a standard module, and look at the Immediate window for an error.
Run-time error 1004. Usually an invalid column reference, a misspelled sheet name, or a protected sheet. Verify the sheet name matches exactly, including spaces, and unprotect it first.
The wrong columns disappear. Add the worksheet reference and print the range you built with Debug.Print before hiding. This shows whether the string is what you expect.
The macro is missing after reopening. The file was saved as .xlsx. Use File > Save As and choose Excel Macro-Enabled Workbook, then re-enter the code.
Excel blocks macros with a security warning. Use the yellow bar's Enable Content option for a file you trust, or move the file to a trusted location set in the Trust Center. Never lower macro security globally for a downloaded file.
The columns reappear by themselves. Another macro or a Workbook_Open event may be resetting the layout. Search the project for Hidden = False and check the ThisWorkbook module.
Frequently Asked Questions
Can I hide columns on several sheets at once?
Yes. Loop through a list of sheet names or use Sheets(Array("Jan","Feb")).Select and then Selection.Columns("C").Hidden = True. Looping is clearer and avoids errors when one sheet is protected.
How do I hide columns based on a cell value?
Read the value into a variable and compare it, for example If ws.Range("A1").Value = "Hide" Then ws.Columns("D").Hidden = True. To check many columns, loop from 1 to the last used column.
Does hiding columns with VBA delete the data?
No. Hidden columns keep their values, formulas and formatting, and calculations continue. Only the display changes.
How do I unhide everything with one line?
Use Cells.EntireColumn.Hidden = False on the target sheet. This reveals every column, including ones hidden manually before the macro ran.
Why does my code hide rows instead of columns?
Because a multi-cell range's Hidden property refers to rows. Chain .EntireColumn before .Hidden to switch it to columns.
Can I hide columns without enabling macros?
Not with VBA — the code needs macros to run. If macros must stay off, hide columns manually with the right-click menu or set the column width to zero.