We get data from a vendor where one field has multiple spaces scattered throughout the value. We want to replace all occurences of multiple spaces with just a single space. Running the Replace function once does not get rid of all occurences. We tried using a function to repeatedly replace all double spaces with single spaces and it blanked the entire value. What's a better way to code the following function?
Function ReduceSpaces(MyStr As Variant) As Variant
On Error GoTo ReduceSpacesError
Do Until InStr(MyStr, " ") = 0
MyStr = Replace(MyStr, " ", " ")
Loop
ReduceSpacesEnd:
Exit Function
ReduceSpacesError:
MsgBox Error$
Resume ReduceSpacesEnd
End Function
Function ReduceSpaces(MyStr As Variant) As Variant
On Error GoTo ReduceSpacesError
Do Until InStr(MyStr, " ") = 0
MyStr = Replace(MyStr, " ", " ")
Loop
ReduceSpacesEnd:
Exit Function
ReduceSpacesError:
MsgBox Error$
Resume ReduceSpacesEnd
End Function