Excel VBA Set All Columns Width
In this article
Yes, you can set all columns to the same width in Excel using VBA by looping through the used range and applying the ColumnWidth property to each cell. This is most efficient by setting the width once on the entire range, rather than iterating through every single cell individually.
What You'll Need
- Microsoft Excel installed on your computer. The code below works in the standard VBA editor included with all desktop versions of Excel (Windows and macOS).
- Basic familiarity with the Excel interface. You should know how to open the Developer tab and access the Visual Basic for Applications (VBA) editor.
- A spreadsheet with data. The width adjustment will apply to the used range of the active worksheet. Cells outside of the data will remain unchanged.
Step-by-Step
- Open the Excel Workbook. Launch the Excel application and open the specific workbook where you want to resize the columns. Ensure the data sheet is the active sheet by clicking on its tab at the bottom of the window.
- Open the VBA Editor. If you are on Windows, press Alt + F11 on your keyboard to open the VBA editor. On a Mac, go to the menu bar at the top, select Tools, and choose Macros or Visual Basic Editor. Alternatively, you can right-click a sheet tab and select View Code.
- Insert a New Module. In the VBA editor window, you will see a list of projects on the left. Expand your workbook by clicking the + sign next to it. Right-click on the Modules folder (or the VBAProject name if Modules are not visible) and select Insert > Module. This creates a blank code window for your script.
- Copy and Paste the Code. Paste the following code into the empty module window. This script sets the column width to 20 characters for all used columns in the active sheet.
Sub SetAllColumnWidth() ' Define the desired width in characters Dim desiredWidth As Double desiredWidth = 20 ' Apply the width to the used range of the active sheet ' Using EntireColumn ensures the whole column is resized, even if empty ActiveSheet.UsedRange.Columns.ColumnWidth = desiredWidth MsgBox "All column widths have been set to " & desiredWidth & " characters.", vbInformation End Sub - Run the Macro. You have two main options to execute the code. The first is to click anywhere inside the code block you just pasted and press F5 on your keyboard. The second is to close the VBA editor and return to your Excel sheet. Press Alt + F8 to open the Macro dialog box, select SetAllColumnWidth from the list, and click Run.
- Review the Changes. Excel will execute the command, and the message box will confirm the action. The columns containing your data will now have a uniform width of 20 characters. The message box will automatically close when you click OK.
Common Mistakes
- Applying the width to cells instead of columns. A common error is writing code that targets the
Cellsobject, such asCells(1, 1).ColumnWidth = 20. This line of code attempts to set the width for a specific cell, not the entire column. While this technically works, it is an extremely inefficient way to resize a large spreadsheet. It requires the macro to loop through every single cell in the used range, which can cause the macro to pause or run slowly on large datasets. The correct method is to apply the property directly to theColumnsobject, as shown in the step-by-step guide. - Assuming the width applies to hidden columns. The
UsedRangeproperty returns the range of cells that have ever been used. If you have hidden columns within this range that have no data, the code will not resize them. TheColumnWidthproperty only affects visible columns. If you need to resize hidden columns, you must write a loop that iterates through theColumnscollection and checks if the column is hidden before applying the width. - Not specifying the sheet. If your workbook has multiple sheets, the code above uses
ActiveSheet. This means the width will be set on the sheet you currently have selected when you run the macro. It will not affect other sheets. If you want the macro to run on every sheet in the workbook, you would need to add a loop to iterate through theWorksheetscollection.
Tips
- Use the
.EntireColumnproperty. The code provided usesUsedRange.Columns.ColumnWidth. This is generally the most robust method for standard spreadsheets. However, if your data has a single empty cell at the bottom or right edge,UsedRangemight not capture the full width. A more advanced approach is to useActiveSheet.UsedRange.EntireColumn.ColumnWidth = 20. This ensures that even if the used range is slightly smaller than the data, theEntireColumnproperty expands the selection to cover the full width of every column that has data in any row. - Test on a backup copy. Before running any macro on your main working file, it is a good practice to make a copy of the file. This allows you to run the macro on the backup without risking accidental data loss or formatting issues that might be difficult to undo.
- Combine with other formatting. You can easily combine this command with other formatting tasks in a single macro. For example, you can add a line to set the font size to 11 and the font to Calibri, all in one go. This is useful for creating standardized report templates.
Troubleshooting
If the macro does not appear to change the column widths, check the following common issues.
- The macro ran, but nothing changed. This is often caused by the macro running on the wrong sheet. Double-check that you are on the correct worksheet and that the sheet actually has data in it. If the sheet is completely empty, the
UsedRangewill be zero, and the macro will not perform any action. - The macro is slow or hangs. If you are running the code on a very large dataset (thousands of rows), you might experience a delay. This is because the code is processing the entire used range. If the macro seems unresponsive, wait a few moments. If it does not complete, ensure that no other Excel processes are running in the background that might be consuming system resources.
- The width is set, but the text is cut off. The VBA
ColumnWidthproperty is measured in characters, not inches or millimeters. If your text requires more than the set number of characters to display fully, it will be hidden. To fix this, you can increase thedesiredWidthvariable in the code. You can also use theAutoFitmethod, which lets Excel determine the optimal width for each column based on the longest string of text it contains. This is achieved by changing the code to:ActiveSheet.UsedRange.Columns.AutoFit.
Frequently Asked Questions
Can I resize columns based on the content of the cells?
Yes. Instead of setting a fixed number, you can use the AutoFit method. This command tells Excel to look at the longest piece of text in each column and automatically widen the column just enough to display it without wrapping to the next line.
Does this macro work in Excel Online?
No. The VBA editor and macros are not supported in the browser-based version of Excel. These scripts will only run in the desktop application installed on your computer.
How do I apply this to all sheets in the workbook?
You need to modify the code to loop through the Worksheets collection. You would replace the single line that sets the width with a loop that iterates through each worksheet and sets the width for that specific sheet.
Why does the macro fail if I have merged cells?
Merged cells can sometimes cause issues with the UsedRange property, as it may not accurately define the boundaries of the data. If you encounter this, consider unmerging the cells before running the macro, or use a more specific range definition in your code.