Excel VBA Set Cell Height: Row Height Made Easy
In this article
Yes, you can set cell height in Excel VBA using the RowHeight property of the Range or Rows object. A single line like Range("A1").RowHeight = 25 sets the height of the entire row to 25 points, because Excel adjusts row height, not individual cells in isolation. Below is a complete guide, from opening the VBA editor to auto-fitting rows, plus common errors and fixes.
What You'll Need
- A desktop version of Excel (Windows or Mac) with macros enabled
- A workbook saved as a macro-enabled file (.xlsm) if you want to keep the code
- Basic familiarity with opening the Visual Basic Editor (Alt + F11 on Windows, Option + Fn + F11 on Mac)
Step-by-Step
- Open the Visual Basic Editor. Press Alt + F11 (Windows) or Option + Fn + F11 (Mac). Menu labels and key combos can differ slightly between Excel versions and platforms.
- Insert a module. In the editor, go to Insert > Module. This gives you a place to paste code.
- Add a basic subroutine. Type the following:
Sub SetCellHeight()
Range("A1").RowHeight = 25
End Sub
SettingRowHeighton a single cell applies to the whole row, since every cell in a row shares the same height. - Set the height of a whole row directly. Use the
Rowsobject when you know the row number:Rows(3).RowHeight = 30 - Set multiple rows at once. Specify a range of rows:
Rows("3:10").RowHeight = 20
or:Range("A3:A10").RowHeight = 20 - Auto-fit rows to their content. Instead of a fixed number, let Excel size the row:
Rows("3:10").AutoFit
This is the VBA equivalent of double-clicking the row border in the worksheet. - Run the macro. Press F5 with the cursor inside the subroutine, or press Alt + F8 in the worksheet, choose
SetCellHeight, and click Run. Check the worksheet: the row should now match the height you set, measured in points (there are 72 points in an inch; the default row height is about 15 points). - Save your work. Save the workbook as an .xlsm (macro-enabled) file via File > Save As and choosing Excel Macro-Enabled Workbook, otherwise the code will be discarded.
Common Mistakes
- Trying to set the height of one cell only. Excel has no per-cell height;
Range("B2").RowHeight = 40resizes all of row 2. If you need visual variance, merge cells or adjust borders instead. - Confusing points with pixels.
RowHeightis in points, not pixels. A value of 20 points is not 20 pixels on screen. - Expecting AutoFit to work on merged cells. AutoFit does not size rows correctly for merged cells, so avoid merging across rows if you need auto-sizing.
- Using
ColumnWidthby accident. That controls width, not height, and uses a different unit (character-based). - Forgetting to enable macros. If macros are disabled, nothing happens when you run the code. Enable content via the security warning bar, or through File > Options > Trust Center > Trust Center Settings > Macro Settings.
Tips
- Read a current height the same way:
Debug.Print Range("A1").RowHeightprints it to the Immediate window (Ctrl + G to open it). - To return a row to its default height, use
Rows(3).RowHeight = 15or, more robustly,Rows(3).UseStandardHeight = True. - For big ranges, turn off screen updating first to speed up the macro:
Application.ScreenUpdating = False
Rows("3:500").AutoFit
Application.ScreenUpdating = True - After wrapping text (
Range("A1").WrapText = True), callAutoFiton that row so long text is fully visible. - Set a height for all rows on a sheet with
Rows.RowHeight = 18, or all used rows withActiveSheet.UsedRange.Rows.AutoFit.
Troubleshooting
- Nothing happens when you run the macro. First, confirm you are on the correct worksheet; unqualified
Rows(3)refers to the active sheet. Qualify it to be safe:Worksheets("Sheet1").Rows(3).RowHeight = 25. Then check that macros are enabled and that you ran the correct subroutine. - Runtime error 1004. This usually means the sheet is protected. Unprotect it with
Worksheets("Sheet1").Unprotect(supply the password if one was set), or remove protection manually via Review > Unprotect Sheet. Do not attempt to remove protection from a workbook you do not have the right to edit. - The row snapped back to a different height. If the sheet's rows are set to auto-fit, Excel can recalculate the height after edits. Set an explicit
RowHeightvalue after any content changes to keep it fixed. - Row height looks wrong after pasting data. Excel often auto-fits rows on paste. Re-run your height macro after pasting, or paste with Paste Special > Values to avoid layout changes.
- Code was lost after saving. You likely saved as .xlsx, which strips macros. Re-add the code and save as .xlsm.
Frequently Asked Questions
How do I set row height in Excel VBA?
Use Range("A1").RowHeight = 25 or Rows(3).RowHeight = 25, where the number is the height in points.
Can I set the height of a single cell only?
No. In Excel, height is a property of the row, so any cell you target resizes its entire row. Merging cells is the usual workaround for a visual effect.
What unit does RowHeight use?
Points. There are 72 points in an inch, and the default row height in most workbooks is about 15 points.
How do I auto-fit row height with VBA?
Call Rows("1:20").AutoFit or Range("A1:A20").EntireRow.AutoFit. Avoid merged cells, which AutoFit does not handle correctly.
Why do I get error 1004 when setting RowHeight?
The worksheet is most likely protected. Unprotect it in code with Worksheets("Sheet1").Unprotect or via Review > Unprotect Sheet, then run the macro again.
How do I reset a row to the default height?
Set Rows(3).UseStandardHeight = True, or assign the default point value manually, typically 15.