How to Do Autofit Cells in Excel Using VBScript
- 1). Add the "Developer" tab to the menu ribbon if you haven't already done so. Do this by clicking on the "File" tab on the left end of the menu ribbon. Click on "Options" and then on "Customize Ribbon" in the categories pane. Select "Developer" from the list of main tabs and then click "OK." The "Developer" tab should now appear in the ribbon.
- 2). Click on the Developer tab and then on "Visual Basic" to open the Visual Basic Editor window.
- 3). Insert the following line into your macro's code to use VB script to autofit cells in Excel:
Worksheets("Sheet1").Range("A1:E1").Columns.AutoFit
The information in parentheses after "Worksheets" is the name of your worksheet (usually "Sheet1"). The information in the parentheses after "Range" is the range of columns or rows or the single cell you want to autofit. You can change "Columns" to "Rows" if you want to autofit the height of the cell(s) rather than the width.