Setting Minimums and Maximums when AutoFitting Column Widths

Written by Allen Wyatt (last updated February 20, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

Ken has a worksheet with over 100 columns of varying widths. To make it readable, he selects the whole worksheet (except for the header row) and uses AutoFit Column Width. Some columns have a single digit and he loses the ability to read the header. Some columns have 200+ characters, so they become too wide. Ken wonders if there is a way to set a minimum (7) and maximum (50) column width for the AutoFit function.

This cannot be done natively within Excel as there is no built-in way to set the minimum or maximum. However, it is easy enough to do using a macro. Here's an example of one that will do the trick:

Sub SetColWidths()
    Dim c As Range
    Dim iMin As Integer
    Dim iMax As Integer

    iMin = 7
    iMax = 50
    Application.ScreenUpdating = False
    For Each c In Selection.Columns
        c.AutoFit
        If c.ColumnWidth < iMin Then c.ColumnWidth = iMin
        If c.ColumnWidth > iMax Then c.ColumnWidth = iMax
    Next c
    Application.ScreenUpdating = True
End Sub

In order to use the macro, simply select cells in the columns you want to affect (as Ken does), and then run the macro. It applies AutoFit to the columns and then steps through each one to make sure that nothing is narrow than 7 or wider than 50.

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 (13825) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Repeating Rows for a Table Footer

Word allows you to specify rows that should be repeated at the top of a table when that table extends beyond the bottom ...

Discover More

Dealing with Small Time Values

It is no secret that you can store time values in an Excel worksheet. But do you really know how small of a time value ...

Discover More

WordTips Ribbon 2020 Archive (Table of Contents)

WordTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At ...

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)

Widening a Column to a Particular Cell's Width

Do you want to set a column's width based on whatever is in the currently selected cell? There are actually a number of ...

Discover More

Shortcuts to Hide Columns

Need a quick way to hide and unhide columns in a worksheet? The shortcuts described in this tip can help fill the bill.

Discover More

Unhiding a Single Column

In a worksheet with lots of hidden columns it is a real pain to try to unhide just one or two columns. The best solution ...

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?

2021-02-20 10:30:33

Allen Cody

The above macro requires that columns be selected before execution. The "c.autofit" line can be removed entirely, as it is unnecessary. Change the line, "For Each c In Selection.Columns" to "For Each c In Cells.Columns" so that the subroutine will autofit all columns in the worksheet within the minimum and maximum parameters established - 7 and 50, respectively.


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.