Consulting

Results 1 to 8 of 8

Thread: Search and Replace from the same Array

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Search and Replace from the same Array

    hello folks and evening,

    well i was thinking if it was possible to search and replace from the same array?

    Sub SearchReplaceArray()
    Dim oRng As Word.Range
    Dim arrWords As Variant
    Dim i As Long, j As Long
    arrWords = Array(A, B, C, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    For i = 0 To UBound(arrWords)
        Set oRng = ActiveDocument.Range
        j = 0
       With oRng.Find
            Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                oRng = oReplace(i + 2)
                oRng.Collapse 0
                i = i + 1
            Loop
        End With
    Next i
    Set oRng = Nothing
    End Sub
    is this at all possible, if i have one array and i want to replace using the same array

    example

    A,B,C

    becomes

    1,2,3
    Last edited by Aussiebear; 04-19-2023 at 04:36 AM. Reason: Adjusted the code tags
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrFnd As String, i As Long
    StrFnd = "0,1,2,3,4,5,6,7,8,9"
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .MatchCase = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      For i = 0 To UBound(Split(StrFnd, ","))
        .Text = Split(StrFnd, ",")(i)
        .Replacement.Text = Chr(Asc(Split(StrFnd, ",")(i)) + 17)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Paul,

    thank you for the speedy response



    Now - what if my array has words in it


    oSearch = Array("Green", "Grizzly", "Bob","Apple", "Bear", "Cat")

    Could i substitute words within the same array

    Replace > Green with Apple

    Grizzly with Bear

    Bob with Cat


    with i+2 on the replace

    I know its easier to use 2 arrays, but i am really curious becuase i wanted to add some stuff to the end of an array, and just do my replacement just for convenience really.

    there is something i cant work out on the logic, you can search and replace 1-1 on the same terms but ...


    thanks for the code and help
    Last edited by dj44; 11-12-2017 at 04:32 PM. Reason: As usual im useless at explaining these things
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    That's a disaster waiting to happen. What do you suppose will happen when 'Apple' is found, as it inevitably will be once you've replaced 'Green' with 'Apple'? Hint: 'out of bounds'.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You might do better with something like:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim ArrFnd(), i As Long
    ArrFnd = Array("Green", "Apple", "Grizzly", "Bear", "Bob", "Cat")
    If UBound(ArrFnd) Mod 2 = 0 Then
      MsgBox "Dodgy Array"
      Exit Sub
    End If
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindContinue
      .MatchCase = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      For i = 0 To UBound(ArrFnd) Step 2
        .Text = ArrFnd(i)
        .Replacement.Text = ArrFnd(i + 1)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    With this approach, each Find term is followed by its Replace term and the loop is processed in terms of item pairs.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Well i can make a mess and it wouldnt be the first time at all

    Well i was trying to partition my array into sections

    so i would run the loop between sections

    for example
    My search terms > 0 - 3 "Apple" "Grizzly" etc
    My replace terms > 4 -7 "Green" "Bear"


    i was trying to work out if i could partition my array - that would allow me to search and replace using the same source?

    I am really speculating as i have not found anything yet to model this on
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  7. #7
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thank you Paul, it is making sense now, I could do that
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  8. #8
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    I have also made the basic example as like this

    Sub Search_Replace_Same_Array()
    Dim oRng As Word.Range
    Dim oArray As Variant
    Dim i As Long
    oArray = Array("Search Terms >>>", "AA", "BB", "CC", "DD", "EE", "Replacement Terms put here >>> ", 1, 2, 3, 4, 5, 6, 7, 8, 9)
    For i = 1 To 5
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(FindText:=oArray(i), MatchWholeWord:=True)
                oRng = oArray(i + 6)
                oRng.Collapse 0
            Loop
        End With
    Next i
    Set oRng = Nothing
    End Sub

    but i am happy that 1 array can be used to do the search and the replacements, and i never knew that before

    Well i had a lot of arrays and i wnated to declutter my house so i thought why not use 1 array, if it does the job on the tin

    thank you Paul
    Last edited by Aussiebear; 04-19-2023 at 04:39 AM. Reason: Adjusted the code tags
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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