To hide a column by its number in Excel VBA, set that worksheet column’s Hidden property to True. For example, ws.Columns(3).Hidden = True hides column C, as long as ws refers to the worksheet you intend to change.
The basic VBA method
Excel numbers worksheet columns from left to right, starting with 1 for column A. That makes it possible to use a column number directly with the worksheet’s Columns collection. Here is a complete macro that hides column C on a sheet named Data:
Save a copy of your workbook before running a macro. VBA changes cannot be undone with Ctrl+Z.
Sub HideColumnByNumber()
Dim ws As Worksheet
Dim columnNumber As Long
' Use the worksheet that contains the column to hide.
Set ws = ThisWorkbook.Worksheets("Data")
columnNumber = 3
ws.Columns(columnNumber).Hidden = True
End Sub
Change 3 to the column number you need, and change Data to the exact worksheet tab name. The macro hides the entire column, not just the cells containing information. It does not delete or clear the contents.
How column numbers map to letters
The number is the column’s position on the worksheet, not a number stored in a cell and not the row number. The first three mappings are A = 1, B = 2, and C = 3. After Z = 26, the sequence continues with AA = 27, AB = 28, and so on. The last column in a modern Excel worksheet is XFD, number 16,384.
This numbering is useful when the target column is calculated or supplied as a number. For a fixed, familiar column, using its letter can be easier to read: ws.Columns("C").Hidden = True also hides column C. The numeric and letter forms both refer to whole columns when used with the worksheet’s Columns collection.
- Use
ws.Columns(1)for column A. - Use
ws.Columns(10)for column J. - Use
ws.Columns(27)for column AA. - Use a number from 1 through 16,384 for a valid column in a modern Excel worksheet.
Column numbers are not the same as column letters written as text. If a variable contains the letter C, pass the string form, such as ws.Columns("C"). If it contains the number 3, pass the number, such as ws.Columns(3).
Make sure the macro targets the right worksheet
Qualifying Columns with a worksheet reference is an important reliability habit. In the example, ws.Columns(columnNumber) acts on the sheet assigned to ws. By contrast, an unqualified reference such as Columns(3).Hidden = True acts on the active sheet. If a user clicks another sheet before the macro runs, that unqualified version may hide a column in the wrong place.
ThisWorkbook means the workbook containing the VBA code. It is often suitable when the macro is saved in the workbook being edited. If the code is in a personal macro workbook or an add-in and should change the workbook currently selected by the user, the workbook reference may need to be different. Choose deliberately rather than relying on whichever workbook or sheet happens to be active.
The sheet name inside Worksheets("Data") must match the worksheet tab name, including spaces and punctuation. If the worksheet is named Monthly Report, for example, write Worksheets("Monthly Report"). A misspelled or nonexistent name causes a run-time error before the column is hidden.
Enter and run the macro in Excel for Windows
- Save a copy of the workbook first. If you want to keep the macro in the file, save it as an Excel Macro-Enabled Workbook (
.xlsm). - Open the Visual Basic Editor from the Developer tab by choosing Developer > Visual Basic. If Developer is not visible, enable it through File > Options > Customize Ribbon.
- In the editor, select the workbook project, then choose Insert > Module.
- Paste the complete procedure into the module and edit the sheet name and column number.
- Return to Excel and choose Developer > Macros, select
HideColumnByNumber, and choose Run. You can also run a selected procedure from the Visual Basic Editor with F5.
Excel for Mac also supports VBA macros in the desktop application, though menus and keyboard shortcuts can differ from Windows. Excel for the web can open a macro-enabled workbook, but it cannot create, edit, or run VBA macros; use desktop Excel to run this code. Macro security settings can also prevent a macro from running, so only enable macros in files you trust.
Unhide a column or toggle its visibility
To reverse the change in VBA, set the same column’s Hidden property to False. This example unhides column C on the Data worksheet:
Sub UnhideColumnByNumber()
Dim ws As Worksheet
Dim columnNumber As Long
' Use the same worksheet and column number that were hidden.
Set ws = ThisWorkbook.Worksheets("Data")
columnNumber = 3
ws.Columns(columnNumber).Hidden = False
End Sub
You can also toggle the current state, which hides a visible column and shows a hidden one. A toggle is convenient for a button, but it is not suitable when the macro must guarantee a particular final state. Running a toggle twice reverses its first action.
Sub ToggleColumnByNumber()
Dim ws As Worksheet
Dim columnNumber As Long
' Toggle column C on the Data worksheet.
Set ws = ThisWorkbook.Worksheets("Data")
columnNumber = 3
ws.Columns(columnNumber).Hidden = Not ws.Columns(columnNumber).Hidden
End Sub
When debugging, remember that a column can remain hidden even after the macro finishes. Check the column letters on either side of the hidden area, or run an explicit unhide macro. Hiding changes visibility; it does not remove data, formulas, or formatting.
Hide multiple columns or choose one from a value
To hide a consecutive span by number, loop through its starting and ending column numbers. This example hides columns C through E. Both the first and last numbers are included in the loop:
Sub HideColumnsThreeToFive()
Dim ws As Worksheet
Dim columnNumber As Long
' Hide columns C, D, and E on the Data worksheet.
Set ws = ThisWorkbook.Worksheets("Data")
For columnNumber = 3 To 5
ws.Columns(columnNumber).Hidden = True
Next columnNumber
End Sub
For a nonconsecutive set, repeat the assignment for each desired number or loop over a collection of numbers. Keep the list clear: a hard-coded 3, 5, and 8 means columns C, E, and H, not the third, fifth, and eighth rows.
If the column number comes from a worksheet cell, read the cell’s value into a variable and validate it before using it. The value should be a whole number from 1 through ws.Columns.Count. A blank, text value, zero, negative number, or number greater than the worksheet’s column count should not be passed unchecked to Columns. Checking the input helps prevent run-time errors and makes the macro’s behavior easier to understand.
Common errors and practical checks
- Wrong sheet changes: Qualify the column with a worksheet variable, rather than relying on the active sheet.
- Invalid column number: Column numbers start at 1, not 0. In current Excel worksheets, the maximum is 16,384.
- Sheet name error: Check the spelling of the worksheet tab name and whether the macro is referring to the intended workbook.
- Macro will not run: Confirm that the file is open in desktop Excel, the code is in a standard module, and macro security allows trusted code to run.
- Column appears missing: It may be hidden rather than deleted. Unhide it or inspect the neighboring column letters.
A further distinction matters when working with filtered lists. Hiding a column with Hidden = True is not the same as filtering rows. It also differs from setting a column’s width to zero, even though both can make a column appear absent. Using the Hidden property expresses the intended operation directly and makes it straightforward to reverse.
What hiding does—and what it does not do
Hiding is a display change, not a security feature. People with access to the workbook can often unhide a column, and its values may still be referenced by formulas, charts, or other workbook features. Do not hide confidential information as a way to protect it. Use appropriate file access controls and Excel protection features for the actual security requirement.
Hiding a column also does not remove it from calculations. For example, a formula that refers to a hidden cell can continue to calculate normally. If the goal is to remove data, use a deliberate deletion or clearing operation instead, taking care to understand the effect on formulas and other references. A macro that only sets Hidden to True leaves the underlying cell contents in place.
Frequently Asked Questions
What VBA code hides column B by number?
Use ws.Columns(2).Hidden = True, where ws is set to the worksheet you want to change. Column B is number 2 because column A is number 1.
Can I use a column letter instead of a number?
Yes. Use a string such as ws.Columns("B").Hidden = True. The number form is useful when the column position is stored or calculated as a number; the letter form can be clearer for a fixed column.
How do I unhide a column with VBA?
Set its Hidden property to False, such as ws.Columns(2).Hidden = False. Use the same worksheet reference and column number as the hide operation.
Does hiding a column delete its contents?
No. It changes whether the column is displayed. Cell values and formulas remain in the worksheet, and formulas can continue to use them.
Why does my macro hide the column on another sheet?
The code may use an unqualified Columns(2) reference, which acts on the active sheet. Set a worksheet variable and use ws.Columns(2) so the target is explicit.
Can Excel for the web run this VBA macro?
No. Excel for the web does not run VBA. Open the workbook in the desktop version of Excel for Windows or Mac to use the macro.
Related
- Excel Page Break Dotted Lines: How to Move Them in Print
- Excel Merge Cells and Keep All Text Without Data Loss
- Excel Merged Cells: Do They Need to Be the Same Size?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Find Cell: Code, Options, and ExamplesIn Excel VBA, use the Range.Find method to locate a cell containing specified text or a value. Set…
- Excel VBA Range of Worksheet Failed: Causes and FixesThe message Method 'Range' of object '_Worksheet' failed is usually Excel’s Run-time error 1004: VBA could not resolve…
- Excel VBA Copy Paste Special: Values, Formats, and MoreIn Excel VBA, copy a range and call PasteSpecial on the destination, specifying what to paste. For example,…
- Excel VBA INDEX: Return Values from a RangeIn Excel VBA, “INDEX” usually means Excel’s INDEX worksheet function, called as Application.WorksheetFunction.Index(...) or Application.Index(...) to return an…
- Microsoft Office Macro Recorder: How It WorksMicrosoft Office has macro recorders in desktop Excel and Word; in Excel for Microsoft 365 on Windows, choose…
- Excel VBA Range Syntax: Examples and Best PracticesIn Excel VBA, a range is a cell or group of cells, usually referenced with Range("A1") or Range("A1:C5").…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.