VBA to Remove Leading Space

Mal4131

New Member
Joined
Oct 13, 2016
Messages
16
I am not well versed on macros and am looking for assitance on a VBA that would remove/trim leading spaces. Cell range is A1:AU500. I've googled around and searched the boards here, but I can't quite get anything to work. Thanks for your help.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
One way

Code:
Dim c As Range
For Each c In Range("A1:AU500")
    c = Trim(c.Value)
Next
 
Last edited:
Upvote 0
If it is literally just leading spaces you want removed (and not trailing spaces as well) then a tiny amedment to JLGWhiz's code...

Code:
Dim c As Range
For Each c In Range("A1:AU500")
    c = [COLOR="#FF0000"]L[/COLOR]Trim(c.Value)
Next
 
Upvote 0
Thank you JLGWhiz and Mark858. When attempting to run the VBA I receive the following error message: "Compile error: Invalid outside procedure". Any thoughts? Thanks again.
 
Upvote 0
Thank you JLGWhiz and Mark858. When attempting to run the VBA I receive the following error message: "Compile error: Invalid outside procedure". Any thoughts? Thanks again.
You need to put the posted code inside a macro housing. If you do not have your own macro to add it to, here is what it would look like as a standalone macro...
Code:
Sub TrimLeadingSpaces()
  Dim c As Range
  For Each c In Range("A1:AU500")
    c = LTrim(c.Value)
  Next
End Sub
 
Last edited:
Upvote 0
Thanks Rick! This works; I thought I was doing this earlier, but must have missed something. Quick question: If I also wanted to trim leading zeros from the same range, would I need a separate macro or could the code be incorporated/ built on the one above? Thanks again for your help.
 
Upvote 0
Thanks Rick! This works; I thought I was doing this earlier, but must have missed something. Quick question: If I also wanted to trim leading zeros from the same range, would I need a separate macro or could the code be incorporated/ built on the one above? Thanks again for your help.
I think this one macro would handle both situations...
Code:
Sub TrimLeadingZerosAndSpaces()
  Dim c As Range
  For Each c In Range("A1:AU500")
    c = Replace(LTrim(Replace(c.Value, "0", " ")), " ", "0")
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top