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!
There are a variety of ways that you might want to count the cells in your worksheet. One way is to figure out how many ...
Discover MoreGot a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.
Discover MoreNeed to know how many characters there are in a workbook? You can find out easily with the handy macro introduced in this ...
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