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.
Written by Allen Wyatt (last updated November 30, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
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:
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.
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!
Excel allows you to create a special type of macro called a user-defined function (UDF). These can let you add to the ...
Discover MoreIf you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...
Discover MoreWhen creating a workbook that will be used by others, you may wish to ensure that the user fills in some cells before ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-11-30 12:31:43
J. Woolley
The Tip refers to the ColumnWidth property as "the width of the column in characters, based on the current font settings." It is actually based on the width of the character 0 (zero) in the Normal style.
My Excel Toolbox includes the following macros related to this subject:
SetColumnWidthPixels - Set the width in pixels for all columns in Selection
SetColumnWidthByCell - Make the width of ActiveCell's column fit its contents
AdjustColumnWidths - Adjust the width of selected columns by a fixed increment or a proportional multiplier
See https://sites.google.com/view/MyExcelToolbox/
For more on this subject see https://excelribbon.tips.net/T006242 and https://excelribbon.tips.net/T011302
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments