6

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?

2
  • Thanks, that worked perfectly! Could you just explain why removing the zero and square brackets made a difference? Commented Apr 10, 2018 at 10:47
  • See below. Also, \s{2,0} is an invalid pattern, just try it at regex101.com Commented Apr 10, 2018 at 10:49

2 Answers 2

5

The [\s{2,0}] pattern matches any single whitespace, {, 2, ,, 0 or } chars since these are inside a character class, inside [...]. See the character class reference.

Once you remove [...], you need to also remove the 0 as you need {2,} to match 2 or more occurrences. See the limiting quantifier reference.

Use

.Pattern = "\s{2,}"

See the regex demo.

Sign up to request clarification or add additional context in comments.

Comments

1

The accepted answer doesn't work with multi-line strings because it smashes newline characters too.

If you need to only crush multiple spaces, like the question title, then use this:

''' Replace multiples spaces with one, because one is enough.
Public Function CrushSpaces(ByVal text As String) As String
    With CreateObject("VBScript.RegExp")
        .Pattern = " {2,}"
        .Global = True
        .MultiLine = True
        
        CrushSpaces = .Replace(text, " ")
    End With
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.