Remove Extra Spaces from Selection Using Macros (VBA) in Excel!!
Tags: removeextraspacesine, vbamacros
Published: 2021-02-11 08:10:51

---
title: Remove Extra Spaces from Selection Using Macros (VBA) in Excel!!
published: true
date: 2021-02-09 12:12:05 UTC
tags: RemoveExtraSpacesinE,VBAMacros
canonical_url: https://geekexcel.com/remove-extra-spaces-from-selection-using-macros-vba-in-excel/
---

If you want to remove extra spaces from a selected range in Excel, then we will help you to do that. Today, we are going to see how to **remove extra spaces from the selection using Macros** ([VBA](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/)) in the Excel worksheet. Macros will complete this task quickly with the simple steps. Let’s try out them.

## Remove Extra Spaces from the Selection using Macros:

To achieve this task, you need to follow the below instructions.

- In the following screenshot, you can see the example selection range from **B1:B5**. We are going to delete the extra spaces from this range.

![Example](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/02/EXCEL_7FyDwsmfsW.png)<figcaption id="caption-attachment-21755">Example</figcaption>

- On the **Developer Tab, select** the **Visual Basic** option under the **Code** section.

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

- Then you need to copy and paste the code given below.

VBA code to delete extra spaces using Macros

Sub RemoveExtraSpaces()

Dim range As Range

Dim cell As Range

Set range = Selection

For Each cell In range

If Not IsEmpty(cell) Then

cell = Trim(cell)

End If

Next cell

End Sub

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

![Save the code](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/02/EXCEL_D8DX1BBR3W.png)<figcaption id="caption-attachment-21756">Save the code</figcaption>

- You have to open the sheet containing the data. 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-2.png)<figcaption id="caption-attachment-14747">Choose the Macros option</figcaption>

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

![Click the Run button](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/02/EXCEL_1UBrpS1zKO.png)<figcaption id="caption-attachment-21757">Click the Run button</figcaption>

- Finally, you will see that the extra spaces have been removed from the selected input range as shown in the below image.

![Output](https://eadn-wc04-371788.nxedge.io/cdn/wp-content/uploads/2021/02/EXCEL_ASsuxT9Or1.png)<figcaption id="caption-attachment-21758">Output</figcaption>

## Closure:

In this post, we have described the simple steps to **remove extra spaces from the selection using Macros** ([VBA](https://geekexcel.com/excel-office-365-vba-overview-definition-and-examples/)) in Excel Office 365. Leave your **queries/feedback** in the below comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!

### Keep Reading:

- [Remove Characters at the Start using Macros (VBA) in Excel 365!!](https://geekexcel.com/remove-characters-at-the-start-using-macros-vba-in-excel-365/)
- [Reverse Text Function using Macros (VBA) in Excel Office 365!!](https://geekexcel.com/reverse-text-function-using-macros-vba-in-excel-office-365/)
- [Create a Table of Contents using Macros (VBA) in Excel Office 365!](https://geekexcel.com/create-a-table-of-contents-using-macros-vba-in-excel-office-365/)