DEV Community

Andrews
Andrews

Posted on • Originally published at geekexcel.com on

Autofit Rows using Macros (VBA) in Excel Office 365!!

Autofit Rows using Macros (VBA) in Excel:

In this article, we are going to see the steps to Autofit Rows using Macros in Excel Office 365 with an example. Let’s get into this article!! Get an official version of MS Excel from the following link: https://www.microsoft.com/en-in/microsoft-365/excel

Steps to Autofit Rows using Macros:

To autofit rows in an Excel spreadsheet using Macros, follow the below steps.

  • In the below example, you can see the rows that are not in the proper order. So we are going to autofit rows using the VBA & Macros.

Example
Example

  • Go to the Developer Tab, choose the Visual Basic option in the Code section.

Select Visual Basic option
Select Visual Basic option

  • You need to activate the sheet where you want to autofit the rows.
  • Now, enter the below code.

VBA code for autofit rows

Sub AutoFitAllRows()

Cells.Select

‘Now, you can see the result!!

Cells.EntireRow.AutoFit

End Sub

  • You need to save the code by selecting it. Then close the window.

Enter the Code
Enter the Code

  • Then, open the sheet that contains the data, and on the Developer Tab, ** choose the Macros option in the Code** section.

Choose Macros option
Choose Macros option

  • You have to make sure that your Macro is selected and click the Run button.

Macro dialog box
Macro dialog box

  • Now, all the rows are automatically fitted in the Excel spreadsheet.

Result
Result

Verdict:

In this article, you can clearly understand the steps to Autofit Rows using Macros in Excel Office 365 with an example. Leave your suggestions in the below comment section. Thanks for visiting Geek Excel. Keep Learning!!

Top comments (0)