Consulting

Results 1 to 7 of 7

Thread: Many Wild Card Searches - For different Texts - Array

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

    Many Wild Card Searches - For different Texts - Array

    Hi folks,
    Good Monday.

    I have been pondering this dilemma over the weekend and need some advice.

    Lets say I have multiple different wild card searches I need to run.

    Can I do that from one script?

    I hate to have to have 10 different wild card scripts.

    So for example
    Each wild card search targets a different text and applies some color to it





    Sub MultipleWildCardsSearch()
    
    
        Dim oRng As Word.Range
        Dim lngIndex As Long
    
        Dim oArraySearch() As String
        Dim varLongColors 
    
        'Wild Cards Search  -Can I store in an array or Select Case Statement?
    
         oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")
        
        
        varLongColors = Array(RGB(0, 156, 250), RGB(256, 0, 0))
        
        
        For lngIndex = 0 To UBound(oArraySearch)
        Set oRng = ActiveDocument.Range
        With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = oArraySearch(lngIndex)
        '.Format = True
        .MatchWholeWord = True
    
       'Find the wild card text
    
       .Replacement.Text = oArraySearch(lngIndex)
        .Replacement.Font.Color = varLongColors(lngIndex)
        
     
        .Execute Replace:=wdReplaceAll
        End With
    Next
    lbl_Exit:
    Exit Sub
    End Sub


    What would be the best way for me to set up something like this. I wasn't sure if this was a loop within a loop or something

    Many thanks for your time
    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
    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 these threads they do are very impressive with an excel spreadsheet - i could store loads of these things in there,

    It will take me time to slowly set these up and get togthere the various search fields i wanted to target.

    Now i usually have 2 left feet when it comes to vba coding and can make a right mess.

    Just as a baby step as well , so that i dont go off track with this idea of the search within an array as its quite hard to think like that for non techy folk

    in the interim as well, i am curious to know about this loop array.

    I have an array, but i never thought of before how to make it store a search criteria as

    Search Criteria 1
    Search Criteria 2
    Search Crietria 3

    and then run it from 1 script

    It will help me to understand in the future to try and add it to other basic scripts so i can cut down on 10 searches for a paragraph or something else.


    oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")

    .Text = oArraySearch(lngIndex) << Is this meant to be a Search within a search ?

    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
    oArraySearch(lngIndex) is simply the reference to the relevant element in the array. So, if lngIndex = 0, oArraySearch(lngIndex) = oArraySearch(0) and returns ("ZX[0-9]"

    As for:
    Dim oArraySearch() As String
    ...
    oArraySearch = Split("ZX[0-9]", "\#b[0-9]", ",")
    you might do better to use:
    Dim oArraySearch
    ...
    oArraySearch = Array("ZX[0-9]", "\#b[0-9]", ",")
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

    on this basic script I am getting a type mismatch
    on this line

    .Replacement.Font.Color = varLongColors(lngIndex)

    Sub MultipleWildCardsSearch()
    
    
        Dim oRng As Word.Range
        Dim lngIndex As Long
        Dim oArraySearch
        Dim varLongColors
        
        oArraySearch = Array("ZX[0-9]", "*Q[0-9]", ",")
        
        varLongColors = Array(RGB(0, 156, 250), RGB(256, 0, 0))
        
        
        For lngIndex = 0 To UBound(oArraySearch)
        Set oRng = ActiveDocument.Range
        With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        
        .Text = oArraySearch(lngIndex)
        '.Format = True
        .MatchWholeWord = True
        .Replacement.Font.Color = varLongColors(lngIndex)
        
        .Replacement.Text = oArraySearch(lngIndex)
        .Execute Replace:=wdReplaceAll
        End With
    Next
    lbl_Exit:
    Exit Sub
    End Sub
    well i looked at it for a long time , and i know I did something but i cant work it out now
    Cheers for your help

    dj

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


  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by dj44 View Post
    on this basic script I am getting a type mismatch
    on this line

    .Replacement.Font.Color = varLongColors(lngIndex)
    Doubtless that's because oArraySearch has more elements (3) than varLongColors (2). Consequently, you'll get a 'subscript out of range' error because of that.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

    I thought that comma was part of the split array function.

    oArraySearch = Array("ZX[0-9]", "*Q[0-9]", ",")

    So i must have tinkered with the wrong part


    well thats sorted now

    thanks for the pointers and the awesome threads Paul
    im still setting them up
    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
  •