Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Resize Row Height: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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) or Option + 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

  1. Open the VBA Editor. In Excel, press Alt + F11 (Windows) or Option + F11 (Mac). The Visual Basic Editor window opens.
  2. Insert a new module. In the Project Explorer, right-click your workbook name, choose Insert > Module. A blank code window appears.
  3. Write a simple macro to set row height. Type the following, replacing the row numbers and height as needed:
    Sub SetRowHeight()
        Rows("5:7").RowHeight = 30
    End Sub
    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.
  4. Run the macro. Press F5 while the cursor is inside the subroutine, or go back to Excel and use Developer > Macros, select SetRowHeight, and click Run. (The Developer tab may need to be enabled in Excel Options.)
  5. Resize rows based on a range. If you want to resize the rows that contain a specific range, use EntireRow:
    Sub ResizeRowsForRange()
        Range("A1:C10").EntireRow.RowHeight = 45
    End Sub
    This sets the height of every row that intersects A1:C10.
  6. AutoFit rows to their content. To let Excel choose the height automatically:
    Sub AutoFitRows()
        Rows("5:7").AutoFit
    End Sub
    Note that AutoFit works only if the row height has not been manually set. If you previously set a fixed height, you must first set RowHeight to Auto or use Rows("5:7").EntireRow.AutoFit after clearing manual formatting.
  7. Loop through rows for conditional resizing. For example, to double the height of rows where column A contains "Total":
    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
    This reads the current height and multiplies it, so it works even if heights vary.
  8. Save your work. Press Ctrl + S (Windows) or Command + S (Mac). If prompted about macros, choose Yes to keep the .xlsm format.

Common Mistakes

  • Using Height instead of RowHeight. Height is a read-only property for rows; you must use RowHeight to set it.
  • Forgetting EntireRow. If you apply RowHeight to a multi-cell range like Range("A1:C10") without EntireRow, Excel raises an error because the range spans multiple rows. Use Range("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 RowHeight on a hidden row does not unhide it. You must set Hidden = False first.

Tips

  • Use Rows("5:7").RowHeight = 30 for a fixed height, or Rows("5:7").AutoFit for content-based sizing.
  • To resize all rows on a sheet, use Cells.RowHeight = 20 or Rows.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 RowHeight to 15 (the typical default) or use Rows("5:7").AutoFit after clearing manual height.
  • Combine with Columns("A:C").ColumnWidth to 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 Rows without qualifying the worksheet, it acts on the active sheet. Use Worksheets("Sheet1").Rows("5:7").RowHeight = 30 to be explicit.
  • AutoFit does nothing. The row may have a manual height set. Set RowHeight to Auto first: Rows("5:7").RowHeight = Auto is not valid VBA; instead, use Rows("5:7").AutoFit after clearing manual formatting via Home > Format > AutoFit Row Height manually, or set RowHeight to 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 with Application.EnableEvents = False before your code and set it back to True after.

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.