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

Adjusting Shadow Settings

Insert a graphic into a document and Word allows you to add a shadow behind the graphic. You can also adjust the ...

Discover More

Replacing Graphics with Graphics

You can use the Find and Replace feature of Word to replace inline graphics with other graphics. This tip explains how ...

Discover More

Errors when Copying References to External Cells

If you copy a cell that contains a reference to external data, do you get an error? It could be due to the complexity of ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Calling a Macro from the Workbook_Open Event

You can run a Personal.xlsb macro from within your Workbook_Open code, but you may get an error if you don't make sure ...

Discover More

Determining the RGB Value of a Color

Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value ...

Discover More

Debugging a Macro

Part of writing macros is to make sure they work as you expect. This involves a process known as debugging. Here's how ...

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 6 - 0?

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.