J
John Smith
I'd like to change all the numbers sandwiched between two
alphabets to subcript.
The code at the end of this article, when applied to "C5H8N2S4Zn",
only changes the digits in C5 and N2 to subscript and leaves H8
and S4 intact.
I monitored rText in the debug mode and found that it was first
"C5H", then "5", then "C". When the code looped back to the first
"Do While", rText became "N2S", not "H8N" as I wanted.
Is there a way to modify the following code so it will first find
"C5H" then "H8N", "N2S", and finally "S4Z"? Thanks.
-----------------------------------------
Sub test()
Dim rtext As Range
With Selection
.HomeKey wdStory
With .Find
Do While .Execute(findText:="[A-z][0-9]{1,}[A-z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rtext = Selection.Range
With rtext.Find
Do While .Execute(findText:="[0-9]{1,}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=False) = True
rtext.Font.Subscript = True
Exit Do
Loop
End With
rtext.MoveStart Unit:=wdCharacter, Count:=-1
rtext.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
End With
End With
alphabets to subcript.
The code at the end of this article, when applied to "C5H8N2S4Zn",
only changes the digits in C5 and N2 to subscript and leaves H8
and S4 intact.
I monitored rText in the debug mode and found that it was first
"C5H", then "5", then "C". When the code looped back to the first
"Do While", rText became "N2S", not "H8N" as I wanted.
Is there a way to modify the following code so it will first find
"C5H" then "H8N", "N2S", and finally "S4Z"? Thanks.
-----------------------------------------
Sub test()
Dim rtext As Range
With Selection
.HomeKey wdStory
With .Find
Do While .Execute(findText:="[A-z][0-9]{1,}[A-z]", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=True) = True
Set rtext = Selection.Range
With rtext.Find
Do While .Execute(findText:="[0-9]{1,}", _
MatchWildcards:=True, _
Wrap:=wdFindStop, Forward:=False) = True
rtext.Font.Subscript = True
Exit Do
Loop
End With
rtext.MoveStart Unit:=wdCharacter, Count:=-1
rtext.MoveEnd Unit:=wdCharacter, Count:=-1
Loop
End With
End With