I have an account statement that needs to be cleansed before doing some work with SQL. I've used a RegEx pattern to remove any punctuation and thought I could also use this to match excessive whitespace between words but I'm having trouble with the replace section of my VBA script.
So far I have:
Sub RemoveSpace()
Dim cell As Range
Range("A2:C2").Select
Range(Selection, Selection.End(xlDown)).Select
With CreateObject("vbscript.regexp")
.Pattern = "[\s{2,0}]"
.Global = True
For Each cell In Selection.SpecialCells(xlCellTypeConstants)
cell.Value = .Replace(cell.Value, " ")
Next cell
End With
End Sub
Although this script matches the spaces it doesn't replace them with just a single space. I think what happens is that Excel replaces each single space it matches with another space rather than treating multiple spaces as "one". Is there a way I could remove all of the multiple spaces and replace them with a single space?
\s{2,0}is an invalid pattern, just try it at regex101.com