To hide a column with VBA in Excel, set that column’s Hidden property to True, such as Worksheets("Data").Columns("C").Hidden = True. Set it to False to show the column again; the examples below show how to target a worksheet, hide several columns, and avoid common macro problems.
How VBA hides a column
Excel represents whether a row or column is hidden with its Hidden property. The property belongs to a range, so VBA can apply it to an entire column, a group of columns, or a range that represents one or more full columns. For example, Columns("C").Hidden = True hides column C on the active worksheet. The setting changes the worksheet’s display; it does not delete the column or its contents.
For reliable code, qualify the column with the worksheet you mean. Otherwise, an unqualified reference such as Columns("C") acts on the active sheet, which might not be the sheet the user expects when a macro runs. A worksheet-qualified reference makes the target explicit. The Hidden property accepts a Boolean value: True hides the column, and False unhides it.
Hiding a column is not the same as deleting it, clearing its contents, or filtering rows. Formulas can still refer to values in hidden columns, and charts or other worksheet features may continue to use those values. If the goal is to keep information private, hiding is not a security measure: users may be able to unhide the column or inspect the workbook’s contents.
Basic macro to hide one column
This macro hides column C on a worksheet named Data. Change the worksheet name and column letter to match the workbook. The code uses ThisWorkbook, meaning the workbook that contains the VBA code.
Sub HideColumnC()
Dim ws As Worksheet
' Refer to the worksheet in the workbook containing this code.
Set ws = ThisWorkbook.Worksheets("Data")
ws.Columns("C").Hidden = True
End Sub
The sheet name must match the worksheet tab name, including spaces and punctuation. If the tab is named Sales Data, use Worksheets("Sales Data"). If the named worksheet does not exist in that workbook, VBA raises an error rather than hiding a column on a different sheet.
To hide a different column, replace C with its letter, for example ws.Columns("F").Hidden = True. You can also use a numeric column index: ws.Columns(3).Hidden = True refers to column C because C is the third column. Letter references are often easier to read in short macros; numeric indexes are useful when the column number is calculated by code.
Hide multiple columns or use a variable
To hide a continuous range of columns, specify both ends of the range. For example, the following statement hides columns B through D on the Data worksheet:
Sub HideColumnsBToD()
Dim ws As Worksheet
' Set the target worksheet before changing its columns.
Set ws = ThisWorkbook.Worksheets("Data")
ws.Columns("B:D").Hidden = True
End Sub
Nonadjacent columns can be handled separately, or combined into a range reference such as ws.Range("B:B,D:D").EntireColumn.Hidden = True. Separate statements can be clearer when the columns have different reasons for being hidden. For example, a report might always hide a helper column but hide a second column only when a particular option is selected.
When the column is determined while the macro runs, use a variable. This example hides the fourth column, D:
Sub HideColumnByNumber()
Dim ws As Worksheet
Dim columnNumber As Long
' Use a numeric index when the target is calculated elsewhere.
Set ws = ThisWorkbook.Worksheets("Data")
columnNumber = 4
ws.Columns(columnNumber).Hidden = True
End Sub
A variable is helpful when a macro finds a column by searching a header or responding to a user choice. Check that the resulting column number is within the worksheet’s valid range before using it, especially if it comes from a cell value or external input.
Hide a column when a header matches
A conditional macro can scan the first row and hide a column whose header is Internal. This is useful when a column may move between workbook versions. The example compares the header without regard to capitalization and trims spaces at either end.
Sub HideInternalColumn()
Dim ws As Worksheet
Dim lastCol As Long
Dim colNum As Long
Dim headerText As String
' Check the header row on the named worksheet.
Set ws = ThisWorkbook.Worksheets("Data")
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
For colNum = 1 To lastCol
headerText = LCase$(Trim$(CStr(ws.Cells(1, colNum).Value)))
If headerText = "internal" Then
ws.Columns(colNum).Hidden = True
End If
Next colNum
End Sub
This code scans from column A through the last non-empty cell in row 1. If there are multiple headers named Internal, it hides every matching column. If the header is on another row, change the row number in both references to row 1. If the header might be blank or generated by a formula, decide whether the scan’s last-column logic matches the layout; a blank cell at the far right does not count as the last used header.
Unhide a column with VBA
To reverse the change, assign False to the same column’s Hidden property. This macro shows column C on Data:
Sub UnhideColumnC()
Dim ws As Worksheet
' Show column C on the specified worksheet.
Set ws = ThisWorkbook.Worksheets("Data")
ws.Columns("C").Hidden = False
End Sub
You can show a range in the same way, for example ws.Columns("B:D").Hidden = False. To reveal every column on a worksheet, use ws.Columns.Hidden = False, but be aware that this also reveals columns intentionally hidden for layout or reporting. Unhide only the columns you intend to restore if the sheet contains a deliberate mix of visible and hidden columns.
Setting a column’s width to zero is sometimes used as a way to conceal it, but the Hidden property expresses the intention more clearly. Avoid mixing width changes and hidden-state changes unless the macro also restores the original widths. Otherwise, a column may become visible but remain too narrow to read.
Run the macro in Excel for Microsoft 365
On Excel for Microsoft 365 for Windows, save a macro-enabled workbook as an Excel Macro-Enabled Workbook (.xlsm) if it needs to retain VBA code. Open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste the procedure into the module. Return to Excel, press Alt+F8, select the macro, and choose Run. You can also run it from the Visual Basic Editor while the cursor is inside the procedure.
On a Mac, Excel desktop supports VBA, though the Visual Basic Editor and shortcut behavior differ. The editor is commonly opened with Option+F11 (on some keyboards, use Fn as well); the Developer tab or Tools > Macro > Visual Basic Editor can also provide access, depending on the Excel setup. Use the Mac version’s macro dialog to run a procedure rather than assuming Windows keyboard shortcuts will match. Excel for the web can open and edit many workbooks, but it cannot create, run, or edit VBA macros; open the file in desktop Excel to use this code.
Macro security settings can block code from running. Only enable macros in a workbook you trust, and follow your organization’s security rules. If you plan to distribute the workbook, explain what the macro changes and provide a non-macro route where practical.
Choose VBA or manual column hiding
For a one-time change, using the Excel interface is usually quicker than writing a macro. Select a column, then choose Home > Format > Hide & Unhide > Hide Columns. To show columns, select the columns on either side of the hidden area and choose Home > Format > Hide & Unhide > Unhide Columns. If the hidden column is at the edge of the sheet, selecting the whole worksheet may be more convenient before choosing Unhide Columns.
VBA is more useful when the same action must happen repeatedly, when the target depends on a condition, or when a button should apply a standard layout. It also reduces repeated manual steps in a report workflow. However, a macro introduces code to maintain and requires users to open a desktop version of Excel with macros permitted. For a workbook shared with people who do not need automation, manual hiding may be simpler.
- Choose manual hiding for a quick, occasional layout change or when recipients should not need macros.
- Choose VBA for repeatable actions, rule-based hiding, or a workbook workflow that already uses macros.
- Choose a filter or grouping when the task is to show and hide records or let users expand and collapse sections, rather than conceal a worksheet column permanently.
Common problems and fixes
The macro runs but the wrong sheet changes. An unqualified Columns reference targets the active worksheet. Set a worksheet variable and use ws.Columns(...), as in the examples. Also confirm the workbook reference: ThisWorkbook means the workbook containing the macro, while ActiveWorkbook means the workbook currently active in Excel. Those are not always the same.
Excel reports that the subscript is out of range. This commonly means the worksheet name in the code does not exactly match a tab in the workbook referenced by the code. Check spelling, spaces, and which workbook contains the sheet. A worksheet’s VBA code name is another option for stable references, but it is different from the visible tab name and must be used in the appropriate VBA form.
The code cannot change a protected sheet. Worksheet protection may prevent changes to column formatting, including hiding or unhiding columns. If protection is required, plan how the macro is authorized to make the change and how the sheet is protected afterward. Do not put a password in shared VBA code as though that made the workbook secure; VBA code and worksheet protection are not substitutes for controlling access to sensitive data.
The column appears not to hide. Check that the code refers to the intended worksheet and that the line actually runs. Also look for a later part of the macro that sets Hidden = False, or for a different routine that resets the layout. In a conditional macro, confirm that the header text matches after trimming and case conversion. A filter, grouped outline, or unusual column-width setting can make the display harder to interpret, so inspect the target column’s hidden state and width separately.
Safety and workbook behavior
Before running a macro that changes a workbook, save a copy of the file. VBA changes cannot be undone with Ctrl+Z, and running a procedure may also clear Excel’s undo history. This is especially important for larger macros that change several columns, alter formatting, or affect multiple sheets. Start with a copy and test the procedure on a small sample workbook when adapting code.
Hidden columns remain part of the worksheet. Formulas can calculate from them, and users who can edit the sheet may be able to reveal them. If a report should omit confidential information, create a separate workbook or export that contains only the information intended for its audience. Hiding columns is a presentation choice, not access control.
It can also be helpful to make macros reversible. Pair a hide procedure with an unhide procedure, or provide a clearly named command that restores the intended layout. Avoid a simple toggle when columns can start in mixed hidden and visible states: toggling a multi-column range based on only one column’s current state may produce an unexpected result. Explicitly setting the desired state is more predictable.
Frequently Asked Questions
What is the VBA code to hide column A?
On the intended worksheet, use ws.Columns("A").Hidden = True, where ws is a worksheet variable set to that sheet. Use False instead of True to show column A again.
How do I hide several columns at once in VBA?
For adjacent columns, use a range such as ws.Columns("B:D").Hidden = True. For columns that are not adjacent, set each separately or use an appropriate multi-area range reference.
Can VBA hide a column based on its contents?
Yes. A macro can inspect a header or cell value, then set the matching column’s Hidden property to True. Make the comparison specific enough to avoid hiding the wrong column, and decide what should happen if there are duplicate or missing matches.
Can I run a VBA column macro in Excel for the web?
No. Excel for the web does not run or edit VBA macros. Open the workbook in Excel desktop for Windows or Mac to use VBA; the macro-enabled workbook can still be stored and shared online.
Does hiding a column protect its data?
No. Hiding changes what is displayed, not who can access the contents. A user may unhide the column or inspect the workbook. Do not rely on hidden columns to protect confidential information.
Related
- Data Validation From a Table in Excel: Setup and Fixes
- Power Query Text to Columns: Split Data Step by Step
- Data Validation Framework: Design, Rules, and Examples
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Macro: Generate a Summary AutomaticallyYes. In Excel for Microsoft 365 on Windows, a VBA macro can read a data table, group rows…
- VBA Input Box for Password: Masking and Safer OptionsVBA’s built-in InputBox cannot mask a password: characters appear as the user types. To show dots or asterisks…
- Excel VBA: Copy and Paste Values Across a WorksheetTo replace formulas with their current results across a worksheet in Excel for Microsoft 365 on Windows, run…
- Excel Macro to Pull Data From Other Worksheets SafelyYes. In Excel for Microsoft 365 on Windows, a VBA macro can read ranges from other worksheets and…
- VBA AutoFilter Delete: Remove Visible Rows Safely in ExcelYes. In Excel VBA, use SpecialCells(xlCellTypeVisible) on the filtered data rows, then delete those rows; leave the header…
- Excel VBA UserForm Parameters: Pass and Return ValuesTo pass parameters to an Excel VBA UserForm, create an instance of the form, call a public setup…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.