Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Is it possible to loop a find and replace array?

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location

    Is it possible to loop a find and replace array?

    I have this find and replace array but I need it to loop until no more of the "find" statements are found. Is this even possible?

    [CODE]'notready loop won't stop
    SubFindReplaceMultiple()
    'Do While??Until??
    Application.ScreenUpdating= True 'False
    Dim ArrFnd(), iAs Long
    ArrFnd =Array("11", "1", “22”, “2”, "33", "3", “44”,"4", "55", "5")
    IfUBound(ArrFnd) Mod 2 = 0 Then
    MsgBox "Somethings Wrong Genious"
    Exit Sub
    End If

    WithActiveDocument.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:=wdFindContinue
    .Execute Replace:=wdReplaceAll
    Next
    'End With
    Application.ScreenUpdating= True
    'Loop 'Until??
    End With
    End Sub
    [/CODE]

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Why do you need an array? More to the point, why do you need a macro? You could do the lot with a single wildcard Find/Replace, where:
    Find = <([1-5])\1>
    Replace = \1
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Thanks for the reply Paul. I should have specified that the numbers were just place holders in the array. The actual array contents will be to get rid of a number of different formatting things like 2 spaces to 1, space return to return, 2 tabs to one, 2 returns to 1 and so on. Just a number of formatting things. The way I have it written it for example it would find 2 spaces and change to one but if there is 3 spaces it will go down to 2 but will not loop back to see that there is sill an instance where 2 spaces still exist and so on for the other find replace pairs as well.

    Is there a way to keep the find replace going until no more of the "find" are found? Loop?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case:
    Sub ArrFndRep()
    Dim ArrFR As Variant, i As Long
    ArrFR = Array("the", "quick", "brown", "fox", "jumped", "over", "lazy", "dog")
    If UBound(ArrFR) Mod 2 = 0 Then
      MsgBox "Something's Wrong Genious"
      Exit Sub
    End If
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Format = False
      .MatchWholeWord = True
      .MatchCase = False
      .MatchWildcards = False
      .Wrap = wdFindContinue
      For i = 0 To UBound(ArrFR) Step 2
        .Text = ArrFR(i)
        .Replacement.Text = ArrFR(i + 1)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    End Sub
    As for:
    find 2 spaces and change to one but if there is 3 spaces it will go down to 2
    that's easily handled in a single pass, replacing any sequence of two spaces with a single space.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Paul thanks for the quick reply. Here is the code you posted with my find and replace terms. Still doesn't work. Many instances not replaced.

    Sub ArrFndRep()
    Dim ArrFR As Variant, i As Long
    ArrFR = Array("  ", " ", vbTab & vbTab, vbTab, " ^p", "^p", vbTab & "^p", "^p", "^p^p", "^p")
    If UBound(ArrFR) Mod 2 = 0 Then
      MsgBox "Something's Wrong Genious"
      Exit Sub
    End If
    With ActiveDocument.range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Format = False
      .MatchWholeWord = True
      .MatchCase = False
      .MatchWildcards = False
      .Wrap = wdFindContinue
      For i = 0 To UBound(ArrFR) Step 2
        .Text = ArrFR(i)
        .Replacement.Text = ArrFR(i + 1)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    End Sub
    Attached Files Attached Files

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Using Wildcards

     
    Option Explicit
    Sub ArrFndRep()
        Dim ArrFR As Variant, i As Long
        
        ArrFR = Array( _
            " {2,}", " ", _
            vbTab & "{2,}", vbTab, _
            vbTab & "{2,}^0013", "^0013", _
            " {1,}^0013", "^0013", _
            "^0013{2,}", "^0013" _
            )
        
        If UBound(ArrFR) Mod 2 = 0 Then
          MsgBox "Something's Wrong Genious"    '   Like it :-)
          Exit Sub
        End If
        
        With ActiveDocument.Range.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Forward = True
          .Format = False
          .MatchWholeWord = True
          .MatchCase = False
          .MatchWildcards = True ' <<<<<<<<<<<<<<<<
          .Wrap = wdFindContinue
          For i = 0 To UBound(ArrFR) Step 2
            .Text = ArrFR(i)
            .Replacement.Text = ArrFR(i + 1)
            .Execute Replace:=wdReplaceAll
          Next
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Paul thanks for the reply. What the heck is "^0013"? It still isn't changing tab return to return.

  8. #8
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Ok I figured it out. in the second line I removed the {2, }. works great now. Thanks for the help fellas.

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Try:
    Sub ArrFndRep()
    Dim ArrFR As Variant, i As Long
    ArrFR = Array("  ", " ", "[^t]{2,}", "^t", "[ ^t]{1,}^13", "^p", "[^13]{2,}", "^p")
    If UBound(ArrFR) Mod 2 = 0 Then
      MsgBox "Something's Wrong Genious"
      Exit Sub
    End If
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Format = False
      .MatchWildcards = True
      .Wrap = wdFindContinue
      For i = 0 To UBound(ArrFR) Step 2
        .Text = ArrFR(i)
        .Replacement.Text = ArrFR(i + 1)
        .Execute Replace:=wdReplaceAll
      Next
    End With
    End Sub
    When using wildcards:
    .MatchWholeWord = True
    .MatchCase = False
    are invalid, so there's no point having them. You also don't need ^0013 - ^13 will do - and you should NOT use ^13 (or ^0013) as the Replace expression; otherwise the affected paragraphs will become one, joined together by what appears as a ¶ character. Having " {2,}" as a Find expression with " " as the Replace expression will reduce all space sequences to 1, whereas your previous post indicated you wanted sequences of 2 and 3 spaces reduced by 1 space each (meaning a 3-space sequence would be reduced to 2 spaces, not to 1 space).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Kilroy View Post
    Paul thanks for the reply. What the heck is "^0013"? It still isn't changing tab return to return.
    That's the ASCII value for a CR - some MS Word special code (like ^p, and ^t) aren't recognized using wildcards

    I wasn't sure about how to interpret some of the F&R's you wanted, so I sort of guessed at the {2,}, but I figured you could sort it out

    'space'{2,} means 'two or more spaces'
    'space'{2,5} means 'two to five spaces'

    These constructs 'sort of' a carry over from Regular Expressions - online help is pretty good

    BTW, IMHO it's worth your while to become a little familiar with MS Word's F&R wildcard option, since I've found that it is 1) powerful, and 2) can greatly simplify your macros
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  11. #11
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Paul_Hossler View Post
    some MS Word special code (like ^p, and ^t) aren't recognized using wildcards
    ^t is, though.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Thanks for the information guys really appreciated. I tried running the new macro and I still end up with what looks like 2 return characters.

  13. #13
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    With Word's formatting display turned on, are you seeing:


    or something else, for example:


    or:


    If you're not seeing anything like that, it's probably just the paragraph before/after spacing.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  14. #14
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    I'm seeing:


    I tried removing space before and after settings before running and got the same result.

  15. #15
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    I know I've marked this thread as solved and did so because ultimately the code you guys helped me with does resolve my current issues but, is it possible loop a find replace macro? or just a line of code that would check to se if anymore instance of the "find" are there before ending the macro?

  16. #16
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Unless there are other characters before the 2nd ¶ (e.g. non-breaking spaces), the Find/Replace macro I posted will take care of those.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Kilroy View Post
    is it possible loop a find replace macro? or just a line of code that would check to se if anymore instance of the "find" are there before ending the macro?
    That's what the macro I posted already does - and replaces them as specified.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    this is what it looks like:


    It looks like 2 paragraph makers but even a separate find replace doesn't recognize two. wonder if the final paragraph marker in a document has the same characteristics?
    Attached Images Attached Images

  19. #19
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    As I said:
    Quote Originally Posted by macropod View Post
    Unless there are other characters before the 2nd ¶ (e.g. non-breaking spaces), the Find/Replace macro I posted will take care of those.
    Without actually seeing the problem document, it's impossible to know for sure what other content might be between the two ¶ characters in your screenshot. Can you attach a document to a post with some representative data (delete anything sensitive)? You do this via the paperclip symbol on the 'Go Advanced' tab at the bottom of this screen.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  20. #20
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    here it is:
    Attached Files Attached Files

Posting Permissions

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