dawnMist
01-12-2015, 02:37 AM
Hello,
Recently, I have been trying to improve my find/replace macros with loops.
I wanted to perform the same replacement for numerous number+units combination.
Sub ReplaceAll(toFind As String, toReplace As String)
'
' Replaces toFind with toReplace.
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = toFind
.Replacement.Text = toReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sub ReplaceForEach()
' units with two-character or unique abbr
doubleUnits = Array("mg", "ml", "mm", "kg", "mL", "µ", "mol", "cm")
For Each Item In doubleUnits
ReplaceAll "([0-9]) " & Item, "\1^s" & Item
Next
End Sub
I managed to put the non-breaking space between the number and particular unit.
This works completely fine.
I wanted to do the same thing for equality operators, so I added the following code into the ReplaceForEach() subprocedure:
ops = Array(Chr(60), Chr(62), ChrW(177), ChrW(8804), ChrW(8805))
For Each Item In ops
ReplaceAll "(" & Item & ")([0-9])", "\1^s\2"
Next
But the code completely ignores the Item from my Array and simply adds a nbsp before each number.
Similar thing happens with the following
ReplaceAll Item & "([0-9])", Item & "^s\1".
I cannot get a hold on this, can somebody please help?
Thank you very much.
Recently, I have been trying to improve my find/replace macros with loops.
I wanted to perform the same replacement for numerous number+units combination.
Sub ReplaceAll(toFind As String, toReplace As String)
'
' Replaces toFind with toReplace.
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = toFind
.Replacement.Text = toReplace
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
Sub ReplaceForEach()
' units with two-character or unique abbr
doubleUnits = Array("mg", "ml", "mm", "kg", "mL", "µ", "mol", "cm")
For Each Item In doubleUnits
ReplaceAll "([0-9]) " & Item, "\1^s" & Item
Next
End Sub
I managed to put the non-breaking space between the number and particular unit.
This works completely fine.
I wanted to do the same thing for equality operators, so I added the following code into the ReplaceForEach() subprocedure:
ops = Array(Chr(60), Chr(62), ChrW(177), ChrW(8804), ChrW(8805))
For Each Item In ops
ReplaceAll "(" & Item & ")([0-9])", "\1^s\2"
Next
But the code completely ignores the Item from my Array and simply adds a nbsp before each number.
Similar thing happens with the following
ReplaceAll Item & "([0-9])", Item & "^s\1".
I cannot get a hold on this, can somebody please help?
Thank you very much.