Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Trim: Remove Extra Spaces Fast

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Excel VBA's Trim function removes leading and trailing spaces from a text string, and it also collapses multiple spaces between words into a single space. It is the quickest way to clean imported data, fix lookup failures caused by stray spaces, and normalise user-entered text before saving or comparing it. You do not need to install anything extra: Trim is built into VBA and works in every desktop version of Excel. The only prerequisite is that you can open the VBA editor and run a macro, which means saving your workbook as .xlsm (macro-enabled) if you want to keep the code.

What You'll Need

  • Excel for Windows or Mac (desktop version). VBA is not available in Excel for the web.
  • A workbook saved as .xlsm if you plan to save macros.
  • Access to the Developer tab, or the ability to press Alt+F11 (Windows) or Option+F11 (Mac) to open the VBA editor.
  • Basic familiarity with selecting a range and running a macro.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. If the Developer tab is not visible, right-click the ribbon and choose Customize the Ribbon, then tick Developer in the right-hand list. Menu labels can differ slightly by Excel version and region.
  2. Insert a new module. In the VBA editor, go to Insert > Module. A blank code window appears. This is where your macro will live.
  3. Write a simple Trim macro. Paste the following code, which trims every cell in the selected range:
    Sub TrimSelection()
        Dim c As Range
        For Each c In Selection
            If Not IsEmpty(c.Value) Then
                c.Value = Trim(c.Value)
            End If
        Next c
    End Sub
    This loops through each selected cell, skips blanks, and replaces the value with its trimmed version. It works on text; numbers are left effectively unchanged because Trim converts them to text, so only run it on columns that should be text.
  4. Close the editor and select your data. Press Alt+Q (Windows) or Cmd+Q (Mac) to return to Excel. Highlight the cells you want to clean. If you select an entire column, the macro will process every used cell in it, which can be slow on very large sheets.
  5. Run the macro. Press Alt+F8, choose TrimSelection, and click Run. Alternatively, assign the macro to a button on the sheet via Developer > Insert > Button and then select the macro.
  6. Verify the result. Check a few cells that previously had leading or trailing spaces. You can confirm with a formula such as =LEN(A1) before and after, or by using =EXACT(A1, TRIM(A1)) which returns TRUE when the cell is already clean.
  7. Save the workbook. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). If you save as .xlsx, Excel will warn that macros cannot be saved and will strip the code.

Common Mistakes

  • Using Trim on numbers. Trim returns a string. If you apply it to numeric cells, Excel may store the result as text, breaking sums and lookups. Restrict the macro to text columns or convert back with Val if needed.
  • Expecting Trim to remove non-breaking spaces. The VBA Trim function removes ordinary spaces (ASCII 32) but not the non-breaking space (ASCII 160) that often comes from web pages. Use Replace(c.Value, Chr(160), " ") before trimming, or a helper function.
  • Forgetting to handle empty cells. Without the IsEmpty check, the macro writes an empty string into blank cells, which can affect formulas that test for blanks.
  • Running on a protected sheet. The macro will fail if the sheet is protected. Unprotect it first (Review > Unprotect Sheet) and re-protect afterwards.
  • Saving as .xlsx. The code disappears when you reopen the file. Always save as .xlsm.

Tips

  • To clean an entire column without selecting it manually, modify the macro to loop from row 1 to the last used row: For Each c In Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).
  • If you need to trim only leading or trailing spaces but keep internal double spaces, use LTrim and RTrim instead of Trim.
  • For a one-off cleanup without VBA, Excel's Data > Text to Columns or the TRIM worksheet function can do the same job. VBA is best when you need to repeat the cleanup on many sheets or files.
  • Test the macro on a copy of your data first. There is no undo for a macro that overwrites cells.

Troubleshooting

If nothing happens when you run the macro, check that you selected cells before running it and that the sheet is not protected. If you see a "Macro cannot be found" message, the code may be in a different workbook or the module may have been removed. If the macro runs but spaces remain, the characters are probably non-breaking spaces; add a Replace step as described above. If Excel becomes unresponsive, you likely selected an entire column on a large sheet; press Esc and run it on a smaller range. If you get a "Type mismatch" error, a cell contains an error value such as #N/A; add On Error Resume Next at the start of the loop or filter those cells out first.

Frequently Asked Questions

Does VBA Trim remove all spaces?

No. It removes leading and trailing spaces and reduces multiple internal spaces to one. It does not remove non-breaking spaces or tabs.

Can I use Trim on a formula result?

Yes, if you assign the result to a cell's value. For example, Range("B1").Value = Trim(Range("A1").Value) writes the trimmed text into B1. The original formula in A1 is unchanged.

Is VBA Trim the same as the worksheet TRIM function?

They behave almost identically for ordinary spaces. The worksheet function also removes some additional space characters, while VBA Trim is limited to ASCII 32.

Will Trim work on numbers stored as text?

Yes, it treats them as text and returns a text string. To convert back to a number, use Val or CDbl after trimming.

Can I undo a Trim macro?

No. Macros do not create undo history. Always keep a backup copy of the sheet before running any macro that overwrites cells.

Does Trim work in Excel for Mac?

Yes, VBA is available in the desktop version of Excel for Mac. The keyboard shortcuts differ slightly, but the function and code are the same.