Excel VBA Worksheet Columns: A How-To Guide
In this article
To work with worksheet columns in Excel VBA, you reference them through the Columns property of a worksheet or range, then act on that reference with methods like Select, Copy, Delete, Insert, AutoFit, Hidden, or ColumnWidth. The shortest working pattern is Worksheets("Sheet1").Columns(3).ColumnWidth = 20 or Worksheets("Sheet1").Columns("C").Delete. Everything else in this guide builds on that one idea: get a valid column reference, then apply a property or method to it.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac). Labels vary slightly by version and platform.
- A workbook saved as .xlsm if you want to keep your macros. Standard .xlsx files discard VBA code on save.
- Basic familiarity with the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac) and the Immediate Window (Ctrl+G) for quick tests.
- A test workbook you can safely modify, because column operations like Delete and Insert change data permanently.
Step-by-Step
- Open the VBA Editor and insert a module. Press Alt+F11 (Windows) or Option+F11 (Mac), then in the Project Explorer right-click your workbook and choose Insert > Module. Code in a standard module is easiest to run and reuse.
- Reference the worksheet first. Always qualify the sheet so the code acts on the right one:
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1"). UsingThisWorkbookkeeps the macro tied to the file that contains it, which is safer thanActiveWorkbook. - Reference a single column by number or letter. Use
ws.Columns(3)for column C orws.Columns("C")for the same column. Both return a Range object representing the entire column, from row 1 to the bottom of the sheet. - Reference a block of columns. Use
ws.Columns("B:D")orws.Range(ws.Columns(2), ws.Columns(4))to work with columns B through D together. A singleColumns("B:D")call is cleaner and faster than looping. - Change the column width. Set
ws.Columns("C").ColumnWidth = 25. To let Excel choose, usews.Columns("C").AutoFit.EntireColumn.AutoFiton a range does the same thing when you start from a cell range. - Hide or unhide a column. Set
ws.Columns("D").Hidden = Trueto hide andFalseto show. This is reversible and does not delete data, so it is the safe option when you only want to tidy the view. - Insert a column. Use
ws.Columns("C").Insert, which shifts existing columns to the right. To insert several at once, usews.Columns("C:E").Insert. Note that Insert on a full column can push data past the last used column, so check the sheet layout first. - Delete a column. Use
ws.Columns("C").Delete. This removes the data permanently, so back up the workbook or test on a copy. To clear contents without removing the column, usews.Columns("C").ClearContentsinstead. - Copy or move a column. Use
ws.Columns("C").Copy Destination:=ws.Columns("F")to duplicate, orws.Columns("C").Cut Destination:=ws.Columns("F")to move. Destination must be a single-cell or single-column reference on the same or another sheet. - Loop through columns. Declare
Dim col As Rangeand useFor Each col In ws.Columns("A:E")to apply the same action to each. For numeric loops,For i = 1 To 5: ws.Columns(i).ColumnWidth = 15: Next iworks and is easy to read. - Store a column reference in a variable.
Dim c As Range: Set c = ws.Columns("C")lets you reuse the reference without re-typing the sheet name. Always useSetfor object variables; plain=will raise an error. - Run and verify. Press F5 with the cursor inside the Sub, or run it from the Macro dialog (Alt+F8 on Windows, Tools > Macro > Macros on Mac). Check the sheet visually and use the Immediate Window with
?ws.Columns("C").ColumnWidthto confirm values.
Common Mistakes
- Using
Columns(3)on the wrong sheet because the code relied on the active sheet. Always qualify with a worksheet variable. - Confusing
Columns(3)withCells(3).Columnstakes a column index or letter;Cellstakes row and column. - Forgetting
Setwhen assigning a Range object, which produces a runtime error. - Deleting a column that other formulas or pivot tables depend on, causing #REF! errors across the workbook.
- Assuming
Columns("C")means only the used range. It means the entire column, so operations likeClearwipe formatting and data all the way down. - Saving as .xlsx and losing the macro. Use .xlsm.
Tips
- Prefer
ClearContentsoverDeletewhen you only want to remove values. - Wrap destructive code in
Application.DisplayAlerts = Falseonly when you are certain, and restore it toTrueafterwards. - Turn off
ScreenUpdatingduring loops over many columns to speed things up, then turn it back on. - Use column letters for readability in small scripts and numbers in loops.
- Test on a copy of the workbook before running any macro that inserts or deletes columns.
Troubleshooting
If the macro does nothing, check that the Sub is in a standard module and that macros are enabled for the file. If you get a "Subscript out of range" error, the sheet name in Worksheets("Sheet1") does not match the tab name exactly, including spaces. If a column operation affects the wrong sheet, you are probably using ActiveSheet implicitly; add an explicit worksheet variable. If Delete produces #REF! errors, undo with Ctrl+Z immediately or restore from a backup. If the macro is slow on large sheets, limit the range with Intersect or work on the used range instead of full columns.
Frequently Asked Questions
How do I reference a column by letter in VBA?
Use Worksheets("Sheet1").Columns("C"). The letter must be in quotes.
What is the difference between Columns and EntireColumn?
Columns on a worksheet returns the whole column directly. EntireColumn is used on a cell or range to expand it to the full column, for example Range("C5").EntireColumn.
Can I hide a column without deleting it?
Yes. Set Hidden = True on the column reference. Set it to False to show it again.
Why does my column width change not stick?
Another macro or a workbook event may be resetting it, or the sheet may be protected. Unprotect the sheet first if you have the password.
How do I loop through only the used columns?
Use ws.UsedRange.Columns in a For Each loop instead of the full sheet columns.
Does Columns work on a protected sheet?
Only if the protection allows column formatting. Otherwise the macro raises an error; unprotect, run the code, then reprotect.