Consulting

Results 1 to 3 of 3

Thread: Skipping Array Terms

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

    Skipping Array Terms

    Good day folks,

    I wanted to understand some logic about array terms

    It’s a bit back wards but I wanted to see how to do this

    Now if the Replace term = "SKIP" completely skip the search term and its execution

     Sub Skip_Array_Terms()
    
    
        Dim oRng As Word.Range
        Dim arrWords As Variant
        Dim oReplace As Variant
        Dim i As Long, j As Long
        
        
        arrWords = Array("A", "B", "C", "D")
    
        oReplace = Array("Apple", "SKIP", "Cat", "SKIP")
    
            For i = 0 To UBound(arrWords)
                Set oRng = ActiveDocument.Range
                j = 0
                With oRng.Find
                    Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                    
                    
                    'IF oReplace(i)="SKIP"     << something like this?
                        
                        oRng = oReplace(i)
                        
                        oRng.Collapse 0
                    Loop
                End With
            Next i
            Set oRng = Nothing
       
        End Sub


    What are the ways I may be able to achieve something like this, if it’s the same word then it is easier a clause somehwere?

    I dont know if i would need to split the array as its the same word on the replace i would like to skip
    Cheers for your help

    dj

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


  2. #2
    I think what you are looking for is

    Sub Skip_Array_Terms()
    Dim oRng As Word.Range
    Dim arrWords As Variant
    Dim oReplace As Variant
    Dim i As Long
    
        arrWords = Array("A", "B", "C", "D")
        oReplace = Array("Apple", "SKIP", "Cat", "SKIP")
    
        For i = 0 To UBound(arrWords)
            If Not UCase(oReplace(i)) = "SKIP" Then
                Set oRng = ActiveDocument.Range
                With oRng.Find
                    Do While .Execute(FindText:=arrWords(i), MatchWholeWord:=True)
                        oRng = oReplace(i)
                        oRng.Collapse 0
                    Loop
                End With
            End If
        Next i
        Set oRng = Nothing
    End Sub
    Note that access to the Arrays is case sensitive.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

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

    Nice to see you.

    Oh yes this is the exact simple line that makes it easy for me to understand.

     
     If Not UCase(oReplace(i)) = "SKIP" Then

    Normally you skip terms on the search, but I didn’t want to split my arrays every time I had to skip a replacement, so it had to be backwards.

    Now let me play about with this, because I do have a feeling seeing how useful it is

    I may make an array of terms in the replacement to skip. eg

    oSkipTerms  = Array("SKIP1", "SKIP2")
     
     If Not UCase(oReplace(i)) = oSkipTerms(j) Then
    So it will be array within array


    I'll pop back later with my new update

    Good Sunday Graham
    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
  •