PDA

View Full Version : [SOLVED:] Remove Spaces After Coma If Preceded By A Number



HTSCF Fareha
09-04-2022, 07:45 AM
I'm struggling to get this Find / Replace to work. It's to try and remove any spaces after a comma when monetry values are found.

Such as this £45, 000 should become thus £45,000 and
£125, 643 should become thus £125,643

Everything I have tried so far either leaves things as per the original state, or removes the preceding numbers and one of the trailing numbers.

Here's what I have:-


Private Sub FixSpacing(CCtrl As ContentControl)
Dim oRng As Range

With CCtrl
Select Case .Type
Case wdContentControlText, wdContentControlRichText
Set oRng = .Range
With oRng
' Remove trailing spaces
Do While .Characters.Last = " "
.Characters.Last.Text = vbNullString
Loop
.End = .End - 1
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True

' Remove single space after comma if it is preceded by a number
.Text = "([0-9],[^s])"
.Replacement.Text = ","
.Execute Replace:=wdReplaceAll

End With
End With
Case Else
End Select
End With

lbl_Exit:
Exit Sub
End Sub

macropod
09-04-2022, 04:07 PM
One wonders why you're even bothering with a macro when all you need is a simple wildcard Find/Replace, where:
Find = ([0-9],)[ ^s]([0-9])
Replace = \1\2

HTSCF Fareha
09-04-2022, 11:52 PM
Many thanks, Paul.

It's needed in a macro as the template is being used on a daily basis and by many users, some of whom are not even aware of the find / replace capabilities of word. It's trying to reduce as many errors as possible.

macropod
09-05-2022, 06:40 AM
In which case, you could incorporate the Find/Replace in another macro you can be fairly sure the users will run. If the errors are restricted to content control ranges, you could simply use a Document_ContentControlOnExit macro to do a Find/Replace on each text content control as it is exited.

HTSCF Fareha
09-06-2022, 12:09 PM
This is part of a sub that is executed to produce a complete document, so this and a couple of others will run by default.