Doing Character Replace

B

Bjk

I have the character sequence "a2M".

I want to do a replace of this in my document (a hundred instances at
least) in which 'a' becomes the alpha character (Symbol font most likely)
and '2' is subscripted.

But if I set 'a2M' for symbol font, EVERYTHING becomes symbol font. And if
I try to set just '2' for subscripting, every character is subscripted.

How is this done?
 
G

Graham Mayor

If you mean you wish to replace a2M with a formatted version then copy the
manually formatted version to the clipboard and replace a2M with ^c

If however you wish to replace sequences *like* a2M with formatted versions
of them you are going to need a macro - something like the following. Add
case statements for each first character's replacement. The second character
is subscripted and the third ignored.

Dim oRng As Range
With Selection
.HomeKey wdStory
With .Find
.ClearFormatting
.Replacement.Text = ""
.Replacement.ClearFormatting
Do While .Execute("[a-z][0-9][A-Z]", _
MatchWildcards:=True)
Set oRng = Selection.Range
Select Case oRng.Characters(1)
Case "a"
oRng.Characters(1).Text = Chr(97)
oRng.Characters(1).Font.name = "Symbol"
Case "b"
oRng.Characters(1).Text = Chr(98)
oRng.Characters(1).Font.name = "Symbol"
'etc
Case Else
End Select
oRng.Characters(2).Font.Subscript = True
Loop
End With
End With

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

macropod

Hi Bjk,

You could do it with a macro like:

Sub ApplySuperscriptNumbers()
Dim oRng As Range, fRng As Range, bState As Boolean
Application.ScreenUpdating = False
Select Case MsgBox("Do you want to process the whole document?", _
vbYesNoCancel + vbQuestion, "Number Superscripter")
Case vbYes
bState = True
Case vbNo
bState = False
Case vbCancel
End
End Select
With Selection
Set oRng = .Range
With .Find
.ClearFormatting
.Text = "[A-Za-z][0-9]{1,}[A-Za-z]"
.MatchWildcards = True
.Wrap = wdFindContinue
.Forward = True
Do While .Execute = True
Set fRng = ActiveDocument.Range(Start:=Selection.Start + 1, End:=Selection.End - 1)
If bState = False Then
If fRng.Start >= oRng.End Then Exit Do
If fRng.End >= oRng.End Then fRng.End = oRng.End
End If
fRng.Font.Superscript = True
fRng.Collapse Direction:=wdCollapseEnd
Loop
End With
End With
oRng.Select
Set fRng = Nothing
Set oRng = Nothing
Application.ScreenUpdating = True
End Sub

Note: As coded, the macro will only work with numbers (of any length) between two or more letters. If that's not quite what you
require, post back.
 
P

Pamelia Caswell via OfficeKB.com

Correct the "a2M" in one place in your document and copy it. In the find
what box, type a2M. In the replace with box type ^c. The "^c" is the code
for the contents of the clipboard.

Pam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top