Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Worksheet Columns: A How-To Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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"). Using ThisWorkbook keeps the macro tied to the file that contains it, which is safer than ActiveWorkbook.
  3. Reference a single column by number or letter. Use ws.Columns(3) for column C or ws.Columns("C") for the same column. Both return a Range object representing the entire column, from row 1 to the bottom of the sheet.
  4. Reference a block of columns. Use ws.Columns("B:D") or ws.Range(ws.Columns(2), ws.Columns(4)) to work with columns B through D together. A single Columns("B:D") call is cleaner and faster than looping.
  5. Change the column width. Set ws.Columns("C").ColumnWidth = 25. To let Excel choose, use ws.Columns("C").AutoFit. EntireColumn.AutoFit on a range does the same thing when you start from a cell range.
  6. Hide or unhide a column. Set ws.Columns("D").Hidden = True to hide and False to show. This is reversible and does not delete data, so it is the safe option when you only want to tidy the view.
  7. Insert a column. Use ws.Columns("C").Insert, which shifts existing columns to the right. To insert several at once, use ws.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.
  8. 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, use ws.Columns("C").ClearContents instead.
  9. Copy or move a column. Use ws.Columns("C").Copy Destination:=ws.Columns("F") to duplicate, or ws.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.
  10. Loop through columns. Declare Dim col As Range and use For 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 i works and is easy to read.
  11. 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 use Set for object variables; plain = will raise an error.
  12. 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").ColumnWidth to 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) with Cells(3). Columns takes a column index or letter; Cells takes row and column.
  • Forgetting Set when 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 like Clear wipe formatting and data all the way down.
  • Saving as .xlsx and losing the macro. Use .xlsm.

Tips

  • Prefer ClearContents over Delete when you only want to remove values.
  • Wrap destructive code in Application.DisplayAlerts = False only when you are certain, and restore it to True afterwards.
  • Turn off ScreenUpdating during 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.