Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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 November 18, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


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, Excel in Microsoft 365, and 2021. 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

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

Discover More

Conditional Processing During a Mail Merge

The Mail Merge capabilities built into Word can appear limited at first glance. One thing that is often overlooked (and ...

Discover More

Word Count in Multiple Selections

Getting a word count for an entire document is easy. What you may not know is that some versions of Word can also provide ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Declaring Variables

Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using ...

Discover More

Automating Copying Macros

You can manually copy macros from one workbook to another, but what if you want to automate the copying process? Here's ...

Discover More

Resizing Checkboxes

If you create a user form in VBA that includes checkboxes, you may want to make the checkboxes larger. You can't adjust ...

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 two more than 7?

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.