Excel VBA Resize Row Height: Step-by-Step Guide
In this article
To resize row height with Excel VBA, set the RowHeight property of a Range or Rows object, for example Rows("5:7").RowHeight = 30 or Range("A1:C10").EntireRow.RowHeight = 45. For automatic fitting, use Rows("5:7").AutoFit. This guide walks through the exact syntax, common pitfalls, and how to make row heights stick.
What You'll Need
- Microsoft Excel (desktop version for Windows or Mac). VBA is not available in Excel for the web.
- A workbook saved as
.xlsm(macro-enabled) if you want to keep your code. - Basic familiarity with the VBA Editor: press
Alt + F11(Windows) orOption + F11(Mac) to open it. - Macro security settings that allow macros to run. If macros are disabled, you'll see a security warning and your code won't execute.
Step-by-Step
- Open the VBA Editor. In Excel, press
Alt + F11(Windows) orOption + F11(Mac). The Visual Basic Editor window opens. - Insert a new module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A blank code window appears.
- Write a simple macro to set row height. Type the following, replacing the row numbers and height as needed:
This sets rows 5 through 7 to a height of 30 points. One point is 1/72 inch; the default row height is typically 15 points.Sub SetRowHeight() Rows("5:7").RowHeight = 30 End Sub - Run the macro. Press
F5while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, selectSetRowHeight, and click Run. (The Developer tab may need to be enabled in Excel Options.) - Resize rows based on a range. If you want to resize the rows that contain a specific range, use
EntireRow:
This sets the height of every row that intersects A1:C10.Sub ResizeRowsForRange() Range("A1:C10").EntireRow.RowHeight = 45 End Sub - AutoFit rows to their content. To let Excel choose the height automatically:
Note thatSub AutoFitRows() Rows("5:7").AutoFit End SubAutoFitworks only if the row height has not been manually set. If you previously set a fixed height, you must first setRowHeighttoAutoor useRows("5:7").EntireRow.AutoFitafter clearing manual formatting. - Loop through rows for conditional resizing. For example, to double the height of rows where column A contains "Total":
This reads the current height and multiplies it, so it works even if heights vary.Sub ConditionalRowHeight() Dim r As Range For Each r In Range("A1:A100") If r.Value = "Total" Then r.EntireRow.RowHeight = r.EntireRow.RowHeight * 2 End If Next r End Sub - Save your work. Press
Ctrl + S(Windows) orCommand + S(Mac). If prompted about macros, choose Yes to keep the.xlsmformat.
Common Mistakes
- Using
Heightinstead ofRowHeight.Heightis a read-only property for rows; you must useRowHeightto set it. - Forgetting
EntireRow. If you applyRowHeightto a multi-cell range likeRange("A1:C10")withoutEntireRow, Excel raises an error because the range spans multiple rows. UseRange("A1:C10").EntireRow.RowHeight = 45. - Setting height on a protected sheet. If the worksheet is protected, you cannot change row height unless the protection allows formatting rows. Unprotect the sheet first or adjust protection settings.
- Assuming AutoFit always works. AutoFit fails if the row height was manually set or if the cell contains merged cells. Merged cells often prevent AutoFit from resizing correctly.
- Using row numbers as strings incorrectly.
Rows("5:7")is correct;Rows(5, 7)is not valid syntax for a range of rows. - Not accounting for hidden rows. Setting
RowHeighton a hidden row does not unhide it. You must setHidden = Falsefirst.
Tips
- Use
Rows("5:7").RowHeight = 30for a fixed height, orRows("5:7").AutoFitfor content-based sizing. - To resize all rows on a sheet, use
Cells.RowHeight = 20orRows.RowHeight = 20. - Row height is measured in points. To convert inches to points, multiply by 72. For centimeters, multiply by 28.35.
- If you need to reset to default, set
RowHeightto15(the typical default) or useRows("5:7").AutoFitafter clearing manual height. - Combine with
Columns("A:C").ColumnWidthto adjust both dimensions in one macro. - Always test macros on a copy of your workbook, especially if they loop through many rows.
Troubleshooting
If your macro does not change row height, check these in order:
- Macros are disabled. Look for a yellow security bar; enable content or adjust Trust Center settings.
- The sheet is protected. Unprotect it via Review > Unprotect Sheet (password may be required).
- You are targeting the wrong sheet. If your code uses
Rowswithout qualifying the worksheet, it acts on the active sheet. UseWorksheets("Sheet1").Rows("5:7").RowHeight = 30to be explicit. - AutoFit does nothing. The row may have a manual height set. Set
RowHeighttoAutofirst:Rows("5:7").RowHeight = Autois not valid VBA; instead, useRows("5:7").AutoFitafter clearing manual formatting via Home > Format > AutoFit Row Height manually, or setRowHeightto a number then AutoFit. - Error 1004 "Unable to set the RowHeight property". This often means the sheet is protected, the range is invalid, or you are trying to set height on a merged cell area. Unmerge cells or unprotect the sheet.
- Height changes but reverts. If the workbook is shared or has event macros (like
Worksheet_Change), another macro may reset heights. Disable events temporarily withApplication.EnableEvents = Falsebefore your code and set it back toTrueafter.
Frequently Asked Questions
How do I set row height for all rows in a worksheet?
Use Cells.RowHeight = 20 or Rows.RowHeight = 20. This applies to every row on the active sheet.
Can I use a variable for row height?
Yes. Declare a variable, e.g., Dim h As Double: h = 25.5, then Rows("5:7").RowHeight = h. RowHeight accepts values from 0 to 409 points.
Why does AutoFit not work on merged cells?
Excel cannot auto-fit rows containing merged cells because the merged area spans multiple columns and rows; you must set a fixed height manually.
How do I resize rows based on text length?
VBA cannot directly measure text length to set height. Use AutoFit and ensure wrap text is enabled; Excel will adjust height to fit wrapped content. If AutoFit fails, calculate an approximate height based on character count and column width, but that is complex and error-prone.
What is the maximum row height in Excel?
409 points. Setting a higher value causes an error.
Can I resize rows in a closed workbook?
No. VBA can only manipulate open workbooks. Open the workbook first, then run your macro.