My row heights are all over the place and I want to make it look more presentable. Instead of adjusting them one by one, did you know you can autofit rows using Macros in Excel? And with this cool trick, it can be done in just one click! Make sure your Excel has the Developer Tab enabled following this tutorial. I explain how you can do this below step by step!
Key Takeaways
-
Quickly Adjust Row Heights – Macros can automatically resize rows to fit the content perfectly, saving manual effort.
-
Ideal for Dynamic Data – Autofit ensures changing or lengthy content remains visible without cutting off text.
-
Enhance Readability in Reports – Proper row height improves the visual layout, making spreadsheets easier to read.
-
Can Be Applied to a Range or Entire Sheet – Macros allow flexibility to autofit selected rows or all rows in the worksheet.
-
Macros Save Time on Repetitive Formatting – Once written, a macro can be reused to instantly autofit rows whenever needed.
Table of Contents
Quick Overview
What does it do?
Autofit all rows to fit to its contents
Copy Source Code:
Sub AutoFitAllRows() Cells.Select 'See the magic happen! Cells.EntireRow.AutoFit End Sub
Final Result:
How to Autofit Rows Using Macros in Excel
STEP 1: Go to Developer > Code > Visual Basic
STEP 2: Make sure Sheet2 is selected as we want to autofit the rows there.
Paste in your code and Select Save. Close the window afterwards.
STEP 3: Let us test it out!
Open the sheet containing the data. Go to Developer > Code > Macros
Make sure your macro is selected. Click Run.
With just one click, all of the rows are automatically fitted now!
Frequently Asked Questions
What does “autofitting rows” mean in Excel?
It means automatically adjusting the height of rows so that all the cell content is fully visible.
Why use a macro to autofit rows?
A macro lets you apply autofit with one click or shortcut, especially useful for large or frequently updated spreadsheets.
Can I autofit only specific rows using a macro?
Yes, you can tailor the macro to target specific rows, a selected range, or the entire worksheet.
Will autofitting affect my formulas or data?
No, it only adjusts the row height—it doesn’t change any data or formulas within your cells.
Is it necessary to know coding to run an autofit macro?
Not necessarily. You can record a macro using Excel’s built-in recorder or copy a simple macro provided online and paste it into the VBA editor.
Bryan
Bryan Hong is an IT Software Developer for more than 10 years and has the following certifications: Microsoft Certified Professional Developer (MCPD): Web Developer, Microsoft Certified Technology Specialist (MCTS): Windows Applications, Microsoft Certified Systems Engineer (MCSE) and Microsoft Certified Systems Administrator (MCSA).
He is also an Amazon #1 bestselling author of 4 Microsoft Excel books and a teacher of Microsoft Excel & Office at the MyExecelOnline Academy Online Course.