Autofit Columns using Macros (VBA) in Excel Office 365!!
Tags: vbamacros, autofitcolumnsusingm
Published: 2020-09-29 04:32:20

---
title: Autofit Columns using Macros (VBA) in Excel Office 365!!
published: true
date: 2020-09-28 12:23:41 UTC
tags: VBAMacros,AutofitColumnsusingM
canonical_url: https://geekexcel.com/autofit-columns-using-macros-vba-in-excel-office-365/
---

## Autofit Columns using Macros (VBA) in Excel:

In this guide, you will understand the steps to **Autofit Columns 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](https://www.microsoft.com/en-in/microsoft-365/excel)
## **Steps to Autofit Columns using Macros:**

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

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

![Example data](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Example-data.png)<figcaption id="caption-attachment-14711">Example data</figcaption>

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

![Choose Visual Basic option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Choose-Visual-Basic-option.png)<figcaption id="caption-attachment-14712">Choose Visual Basic option</figcaption>

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

VBA code for autofit columns

Sub AutoFitAllColumns()

Activate Cells.Select

‘Use this code to get the result!

Cells.EntireColumn.AutoFit

End Sub

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

![Enter the Source Code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Enter-the-Source-Code-2.png)<figcaption id="caption-attachment-14713">Enter the Source Code</figcaption>

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

![Choose the Macros option](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Choose-the-Macros-option.png)<figcaption id="caption-attachment-14714">Choose the Macros option</figcaption>

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

![Macro dialog box](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Macro-dialog-box-4.png)<figcaption id="caption-attachment-14715">Macro dialog box</figcaption>

- Now, all the columns are automatically fitted in the Excel spreadsheet.

### Before running the VBA & Macros Code:

![Example data](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Example-data.png)<figcaption id="caption-attachment-14711">Example data</figcaption>

### After running the VBA & Macros Code:

- ![Result](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2020/09/Result-11.png)

Result

## **Wrap-Up:**

In this guide, we helped you to learn the steps to **Autofit Columns using Macros in Excel Office 365** with an example. Kindly, share your **feedback** in the below comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!!