Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 23 of 23

Thread: Finding matches from a list and replacing from another list, one by one (not global)

  1. #21
    VBAX Regular
    Joined
    Oct 2022
    Posts
    27
    Location
    I have now made a Macro #4 based on Macro #2, with different characters to find and replace.


    Sub Macro4()
    Dim vFindText As Variant
    Dim vReplaceText As Variant
    Dim oRng As Range
    Dim i As Integer, j As Integer, k As Integer, m As Integer
    Dim lAsk As Long
    
    
        vFindText = Array(Chr(176), Chr(186), Chr(111))
        vReplaceText = Array(-3920, -3920, -3920)
        j = 0: k = 0: m = 0
        For i = 0 To UBound(vFindText)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                .Text = vFindText(i)
                Do While .Execute
                    j = j + 1
                    oRng.Select
                    lAsk = MsgBox("Replace Symbol", vbYesNoCancel)
                    If lAsk = 2 Then GoTo lbl_Exit
                    If lAsk = 6 Then
                        k = k + 1
                        oRng.InsertSymbol Font:="Symbol", _
                                          CharacterNumber:=vReplaceText(i), _
                                          Unicode:=True
                    Else
                        m = m + 1
                    End If
                    oRng.Collapse 0
                Loop
            End With
        Next i
    lbl_Exit:
        If j = 0 Then MsgBox "There are no matches", vbInformation
        If k > 0 Or m > 0 Then MsgBox k & " substitutions made" & vbCr & _
           m & " substitutions skipped", vbInformation
        Exit Sub
    End Sub
    This version should find the degree symbol [Chr(176)], the masculine ordinal indicator [Chr(186)], and the letter o [Chr(111)] when it has superscript font formatting, and replace them all with the degree symbol inserted from the Symbol font. How do I specify the superscript font formatting for only the letter o; i.e., only the third item in the find array? (Can this be done by using an additional array to specify font formatting for each item in the find array; e.g., in this case "any" for the first and second items, and "superscript" for the third item?)

  2. #22
    You can add a condition to reflect the position in the array. In the macro, you have three elements i.e. i = 0 to 2. If you want something to apply only to the third element in the array then add a condition

    If i = 2 then do something

    i.e.
    For i = 0 To UBound(vFindText)
            Set oRng = ActiveDocument.Range
            With oRng.Find
                .Text = vFindText(i)
                If i = 2 Then .Font.Superscript = True
                Do While .Execute
                    j = j + 1
                    oRng.Select
                    lAsk = MsgBox("Replace Symbol", vbYesNoCancel)
                    If lAsk = 2 Then GoTo lbl_Exit
                    If lAsk = 6 Then
                        k = k + 1
                        If i = 2 Then oRng.Font.Superscript = False
                        oRng.InsertSymbol Font:="Symbol", _
                                          CharacterNumber:=vReplaceText(i), _
                                          Unicode:=True
                    Else
                        m = m + 1
                    End If
                    oRng.Collapse 0
                Loop
            End With
        Next i
    Last edited by gmayor; 02-19-2023 at 06:22 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #23
    VBAX Regular
    Joined
    Oct 2022
    Posts
    27
    Location
    That works fine. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •