Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Setting Column Width in a Macro.

Setting Column Width in a Macro

Written by Allen Wyatt (last updated July 24, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

It is not unusual to use macros to process data and format output in a workbook. If you use macros to do this type of work, you may be interested in changing the width of a column using a macro. If so, you should pay attention to the ColumnWidth property. This property, when applied to a Column object, indicates the width of the column in characters, based on the current font settings.

For instance, the following code snippet steps through the columns in a selection and sets the width of each column to 10 characters:

For Each c In ActiveWindow.RangeSelection.Columns
    c.ColumnWidth = 10
Next c

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9333) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Setting Column Width in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Find and Replace in a Column or Row

Need to search for information in a table? Word allows you to easily limit your search to an entire column or row, as ...

Discover More

Document is Too Large for Word to Handle

Imagine trying to open a familiar document one day, only to find that Word gives you an error message that the file is ...

Discover More

A Shortcut for Switching Focus

Word provides keyboard shortcuts for lots of things, but it doesn't provide one for switching to the desktop and back to ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Store Common Macros in the Personal Macro Workbook

Want your macros to be available regardless of the workbook on which you are working? Here's how to store them in the ...

Discover More

Clean Up Your Macro List

Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.

Discover More

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 3?

2019-11-03 03:08:40

Thomas Papavasileiou

I frequently use a fairly large columns range with dates in dd/mmm/yy format and the "column width, best fit" option may result in a slightly different column withs, I wrote a macro that checks all the column widths in the range and sets that width to the maximum value registered.

The advantage of this formatting is purely a display that does not change when I scroll to the right of the sheet.

Macro also checks the maximum allowed by Excel column width and has a fancy answer as an ending message

If anyone may be interested, here is the macro

Sub idem_2_max_col_width()
If Selection.Columns.Count < 2 Then
ttl = "What a narrow selection..."
txt = "Selection is one columns wide." & vbCr & vbCr
txt = txt & "Macro ends."
MsgBox txt, vbOKOnly + vbInformation, ttl
Exit Sub
End If

mx = 0
With Selection
.Columns.AutoFit
sc = .Column
ec = .Columns.Count + sc - 1
End With
ad1 = Cells(1, sc).Address
sc_ = Mid(ad1, InStr(ad1, "$") + 1, InStr(2, ad1, "$") - 2)
ad2 = Cells(1, ec).Address
ec_ = Mid(ad2, InStr(ad2, "$") + 1, InStr(2, ad2, "$") - 2)
For i = sc To ec
mx = Application.WorksheetFunction.Max(mx, Columns(i).ColumnWidth)
' Debug.Print i, Columns(i).ColumnWidth
Next
If mx > 254 Then
ttl = "Maximum column width is 255..."
txt = "Macro detects a column larger then 254 and stops" & vbCr
txt = txt & "without any width modification "
MsgBox txt, vbOKOnly + vbCritical, ttl
Exit Sub
End If
Selection.ColumnWidth = mx
ttl = "Succesfull rum..."
txt = "Width of " & ec - sc + 1 & " columns " & sc_ & " to " & ec_ & " is identical and equal to " & mx
MsgBox txt, vbOKOnly + vbInformation, ttl
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.