PDA

View Full Version : [SOLVED:] looping through a String Array



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.

gmayor
01-12-2015, 06:50 AM
The following workesd for me. I suspect your problem is that your strings have a space between the symbol characters and the numbers that you have not allowed for.


Sub ReplaceForEach()
Dim DoubleUnits As Variant
Dim Ops As Variant
Dim item As Variant
' units with two-character or unique abbr
DoubleUnits = Array("mg", "ml", "mm", "kg", "mL", "µ", "mol", "cm", Chr(60), Chr(62), ChrW(177), ChrW(8804), ChrW(8805))
Ops = Array(Chr(60), Chr(62), ChrW(177), ChrW(8804), ChrW(8805))

For Each item In DoubleUnits
ReplaceAll "([0-9]) " & item, "\1^s" & item
Next item


For Each item In Ops
ReplaceAll "(" & item & ")([0-9])", "\1^s\2"
Next item

End Sub

dawnMist
01-12-2015, 07:09 AM
Thank you for your reply.
Yes, the space problem is present as well and it will be addressed later.
However, your code still adds nbsp before every number, not just those preceded by an operator symbol. I do not understand why, everything seems correct to me.

gmayor
01-12-2015, 11:35 PM
OK. I see what you mean. The reason is the reserved charactrer issue I mentioned earlier. The following should avoid that, whether or not there are spaces between the symbol and numbers



Sub ReplaceForEach()
Dim DoubleUnits As Variant
Dim ops As Variant
Dim item As Variant
' units with two-character or unique abbr
DoubleUnits = Array("mg", "ml", "mm", "kg", "mL", "µ", "mol", "cm")
ops = Array("\" & Chr(60), "\" & Chr(62), "\" & ChrW(177), "\" & ChrW(8804), "\" & ChrW(8805))

For Each item In DoubleUnits
ReplaceAll "([0-9]) " & item, "\1^s" & item
Next item

For Each item In DoubleUnits
ReplaceAll "([0-9])" & item, "\1^s" & item
Next item

For Each item In ops
ReplaceAll "(" & item & ")([0-9])", "\1^s\2"
Next item

For Each item In ops
ReplaceAll "(" & item & ") ([0-9])", "\1^s\2"
Next item
End Sub

dawnMist
01-12-2015, 11:58 PM
Oh, I did not realize I had to put the "\" before even when I use the ASCII code, thank you so much for clarifying that!
I am marking this as solved.