Smart Systems for Home

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

Excel VBA Column Width: Set It Right Every Time

· 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

You set column width in Excel VBA with the ColumnWidth property of a Range object: Columns("A").ColumnWidth = 20. That single line sets the width of column A to 20 characters, and you can loop over columns or use AutoFit to size them to their contents. This guide covers the exact syntax, the difference between ColumnWidth and Width, how to autofit, how to set width on multiple sheets, and the mistakes that make macros fail silently.

What You'll Need

  • Excel for Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > Developer on Windows; Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm if you want to keep the macro.
  • Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac) and how to insert a module.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
  2. Write a basic width statement. Type Sub SetWidth(), press Enter, then type Columns("A").ColumnWidth = 20. Press Enter and type End Sub. The number is in character units, not pixels or centimetres: 20 means roughly 20 characters of the default font.
  3. Run it. Place the cursor inside the Sub and press F5, or use Run > Run Sub/UserForm. Switch back to Excel (Alt+F11 again) and check column A. If nothing changed, confirm the workbook you edited is the active one and that macros are enabled.
  4. Set several columns at once. Replace the line with Range("A:C").ColumnWidth = 15 to set A, B and C together, or Columns(3).ColumnWidth = 25 to use a column number instead of a letter. Column numbers count from A=1, so 3 is column C.
  5. Autofit instead of guessing. Use Columns("A:F").AutoFit to size each column to its widest entry. Autofit is the safer choice when text length varies, because a fixed width can hide data or leave huge gaps.
  6. Target a specific sheet. Unqualified Columns refers to the active sheet, which is a common source of surprises. Write Worksheets("Report").Columns("B").ColumnWidth = 12 so the change lands on the sheet you mean, even if another sheet is active.
  7. Loop through columns when the count varies. For a block of data, use For i = 1 To 10: Cells(1, i).EntireColumn.ColumnWidth = 14: Next i. EntireColumn converts a single cell into its whole column, which is useful when you know the header row but not the column letters.
  8. Use points when you need exactness. ColumnWidth is character-based and depends on the workbook's default font. Range("A:A").Width = 100 sets the width in points (1 point = 1/72 inch), which stays consistent across font changes. You cannot set Width on a multi-column range in one go, so loop column by column.
  9. Store and restore widths. If a macro reformats a sheet, save the old value first: oldW = Columns("A").ColumnWidth, then restore with Columns("A").ColumnWidth = oldW at the end. This avoids leaving the user's layout permanently altered.
  10. Save and test on a copy. Save the .xlsm file, then run the macro on a duplicate workbook to confirm it behaves before using it on live data.

Common Mistakes

  • Forgetting the sheet qualifier. Columns("A").ColumnWidth = 20 hits whichever sheet is active. If the macro opens or activates another sheet first, the width lands in the wrong place.
  • Confusing ColumnWidth with Width. ColumnWidth is in characters and varies with the default font; Width is in points. Mixing them gives results that look right on one machine and wrong on another.
  • Setting Width on a multi-column range. Range("A:C").Width = 100 fails or behaves unexpectedly because the range has three columns. Use ColumnWidth for ranges, or loop with Width.
  • Assuming pixels. A width of 20 is not 20 pixels. If you need pixel-level control, convert: pixels are roughly ColumnWidth * 7 + 5 for the default Calibri 11 font, but this varies with font and zoom.
  • Ignoring protected sheets. Setting width on a protected worksheet raises an error unless the sheet is unprotected first. Unprotect, set widths, reprotect.
  • Leaving ScreenUpdating on in loops. Large loops that change widths flicker and slow down. Wrap the loop in Application.ScreenUpdating = False and set it back to True at the end.

Tips

  • Use AutoFit for data-driven sheets and fixed ColumnWidth for dashboards and printed reports where consistent layout matters.
  • Set widths after all data is written, not before, so autofit sees the final content.
  • Cap autofit results: autofit can produce very wide columns for long text. Follow it with a check such as If Columns("A").ColumnWidth > 60 Then Columns("A").ColumnWidth = 60.
  • Remember that column width affects the whole column, not a range of cells. There is no per-cell width in Excel.
  • When distributing a workbook, set widths explicitly rather than relying on autofit, because the recipient's default font may differ.

Troubleshooting

If the macro runs but nothing changes, first confirm macros are enabled and that you edited the right module in the right workbook. Check whether the active sheet is the one you expect, and add the sheet qualifier if not. If you get a "Method 'ColumnWidth' of object 'Range' failed" error, you are probably targeting a multi-area range or a protected sheet: unprotect the sheet and use a single column or a contiguous range.

If the width looks wrong on another computer, the cause is usually the default font or zoom level. Switch to the Width property in points for consistent results, or set the workbook's default font to match. If autofit produces enormous columns, cap the result as shown in the Tips section. If changes vanish after closing, the workbook was not saved as .xlsm, or the code was in a temporary workbook.

Frequently Asked Questions

How do I set column width in VBA?

Use Columns("A").ColumnWidth = 20 for a single column, or Range("A:C").ColumnWidth = 15 for several. Qualify with a worksheet name to avoid acting on the wrong sheet.

What is the difference between ColumnWidth and Width?

ColumnWidth is measured in characters of the default font and works on multi-column ranges. Width is measured in points and applies to one column at a time.

How do I autofit columns with VBA?

Use Columns("A:F").AutoFit. Run it after the data is written so the widest entry is included.

Can I set column width in pixels?

Not directly. Excel exposes characters (ColumnWidth) and points (Width). Convert approximately, but expect small differences between fonts and displays.

Why does my macro set the width on the wrong sheet?

Because unqualified Columns refers to the active sheet. Add the worksheet: Worksheets("Sheet1").Columns("A").ColumnWidth = 20.

Does column width work on a protected sheet?

No. Unprotect the sheet, set the widths, then protect it again.