I've been looking around and haven't found a great answer to the question on how to remove extra spaces from a large range of cells containing text strings. Let's say 5000+ cells.
some ways I have tried include:
For Each c In range
c.Value = Trim(c.Value)
Next c
and
For Each c In range
c = WorksheetFunction.Trim(c)
Next c
and
For Each c In range
c.Value = Replace(c.Value, " ", " ")
Next c
any ideas for speed improvement?
thank you in advance.
I'm usually using Evaluate than loops when it comes on large range. There are so many use of this function, but i won't discuss it further here.
It can depend on many things, but in my case fastest was to get all values at once in array:
Do you have a spare column next to it?
The loop is killing you. This will remove spaces in an entire column in one shot:
Adjust the range to suit. If you want to remove double spaces, then:
EDIT#1:
This version will replace doubles with singles and then check if there are still still doubles left!
You can re-run until you see done
EDIT#2:
based on Don Donoghue's comment, this version will run recursively until all double are converted to singles: