Consulting

Results 1 to 3 of 3

Thread: Target - Specific Arrays - Within Arrays

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

    Target - Specific Arrays - Within Arrays

    Good Friday all,

    Now i have my multidimensional array set up, but i have a snag somewhere

    I can't work out - 1 detail


    if i want to replace array terms in - Array 0

    with array terms in Array 1 - what do i need to do?



    eg

    1,2, 3 becomes A, B,C

    Sub Array_Test()
    
    Dim j As Long
    Dim k As Long
    
    
    oProduct = Array(Array(1, 2, 3), Array(A, B, c), Array(4, 5, 6))
    
    k = 2
    For j = LBound(oProduct) To UBound(oProduct)
        Set oRng = ActiveDocument.Range
        With oRng.Find
            Do While .Execute(oProduct(j)(0), MatchWholeWord:=True)
                oRng.Text = oProduct(k)(j)
                oRng.Collapse 0
            Loop
        End With
    Next j
    
    
    End Sub
    Something about my k's and j's in the loop
    Cheers for your help

    dj

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


  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    Sub Array_Test()
    'A basic Word macro modified by Greg Maxey, http://gregmaxey.com/word_tips.html, 12/8/2017
    '*** Questions\Comments
    '1. Why do you dim some variables but not others?
    Dim oRng As Range
    '2. If j and k are confusing, why do you use them?
    'Dim j As Long
    'Dim k As Long
    Dim lngIndex As Long
    Dim oProduct 'dimmed as variant
    '3. As ArraySortMethods(4,5,6) appears to have no bearing on the desired result, why is it there?
    '4. The following statement, in addition to the apparently superfluous Array(4,5,6) contains variant
        'variables A, B and C (with no value). Even if your code worked the replacement values would be
        'nothing, nothing and nothing.
      'oProduct = Array(Array(1, 2, 3), Array(A, B, C), Array(4, 5, 6))
    '5. Here A, B and C are replaced with literal string values A B and C
      oProduct = Array(Array(1, 2, 3), Array("A", "B", "C"))
      'k = 1
      For lngIndex = LBound(oProduct) To UBound(oProduct(0))
        Set oRng = ActiveDocument.Range
        With oRng.Find
    '      Do While .Execute(oProduct(0)(lngIndex), MatchWholeWord:=True)
    '        oRng.Text = oProduct(1)(lngIndex)
    '        oRng.Collapse 0
    '      Loop
          'Or
          .Execute FindText:=oProduct(0)(lngIndex), MatchWholeWord:=True, ReplaceWith:=oProduct(1)(lngIndex), Replace:=wdReplaceAll
        End With
      Next lngIndex
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

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

    nice to see you this friday!

    Well now i give my usual list of excuses, of course some are ridiculously unvalid!

    well I started off with an array in excel, then i made a multidimensional 1

    So then I transferred it over to word, and somewhere in the haze the array wires got crossed.

    Now yes i pardone me but i forgot to copy over my variables there was a variant some where.

    But needs less to be said , i was confused about my

    dim i

    and then i made a dim k to add in...

    any way its all making as much sense as a dogs dinner.

    Now my Array works nicely because i can add them into 1 master array and then, use 1 at a time

    Well thank you very much for the write up, i do appreciate the short sharp clean code compared to mine

    and happy friday

    folks and all
    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
  •