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

Smart Quotes are Incorrectly Replaced

Not able to replace smart quotes as you want? Here are some ways that you can be sure that every smart quote is changed, ...

Discover More

Comparing Strings

As your macro is processing information, there will doubtless be times that it will need to compare information in ...

Discover More

Quickly Transposing Cells

If you want to turn a range of cells by 90 degrees within a worksheet, you need to understand how Excel can handle the ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Stepping Through a Macro with a Worksheet Visible

When developing a macro, it is often necessary to step through the various code lines so you can see what is happening on ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Storing a User's Location before Running a Macro

Macros are often used to process information in a workbook. If your macro makes changes in what is selected in the ...

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 nine minus 5?

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.