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

Copying Between Instances of Excel

Copying information between two instances of Excel is different than copying information between two worksheets opened in ...

Discover More

Opening Only a Merge Document

After merging the information from a data source into a document, you may decide that you only want to open the merge ...

Discover More

Using the UNIQUE Function

The UNIQUE function can be used to evaluate a range and return the unique values in that range. Understanding how the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Running Macros in the Background

Want to run a macro in Excel, but not sure if doing so will tie up your computer? Here's how macro processing really happens.

Discover More

Bypassing the BeforeClose Event

Hold down the Shift key as you open a workbook, and Excel bypasses any "startup macros" that may be in the workbook. If ...

Discover More

Clearing the Undo Stack in a Macro

Excel keeps track of the actions you take so that you can undo those actions if any are taken in error. You may want to ...

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 + 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.