Consulting

Results 1 to 4 of 4

Thread: Vlookup macro replaced with an array

  1. #1
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location

    Vlookup macro replaced with an array

    Hello,

    I'm trying to replace my old vlookup macro with a more modern version. I first take the table into an array and then compare it to range b first column. How can I get the second element of the dictionary content. I tried ".Items(b(i, 1))(2)", but it did not work.

    a = ActiveSheet.Range("A2:C5").Value
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(a, 1)
        .Item(a(i, 1)) = a(i, 1)
          For ii = 1 To number_of_columns
                    .Item(a(i, 1 + ii)) = a(i, 1 + ii)
          Next ii
        Next
    
    
    b = ActiveSheet.Range("F2:H5").Value
    
    
            For i = 1 To UBound(b, 1)
                If .Exists(b(i, 1)) Then
                    ' "How to get the second element of the existing value??"
                    ' something like?
                    MsgBox .Items(b(i, 1))(2)
                 End If
            Next
    End With
    I appreciate your help and Merry Christmas to you all.
    Last edited by mikke3141; 12-23-2015 at 02:18 PM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
      With CreateObject("Scripting.Dictionary") 
        For each it in ActiveSheet.Range("A2:C5").specialcells(2)
          .Item(it.value) = it.value 
        Next 
      
        For each it in ActiveSheet.Range("F2:G5").specialcells(2)
          if .exists(it.value) then megbox .Item(it.value) 
        Next 
      End With 
    End Sub
    There is no 'second' value
    Last edited by snb; 12-23-2015 at 02:33 PM.

  3. #3
    VBAX Regular mikke3141's Avatar
    Joined
    Jun 2007
    Location
    Klaukkala
    Posts
    53
    Location
    A B C D E F G H
    1 Number Name Age Number Name Age
    2 1 Mat 40 3
    3 2 John 32 2
    4 3 Dave 28
    5 4 Dick 16

    The plan was to populate range(G2:H3) with Dave, 28, John and 32. Did not work

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You might as well create a sample workbook an upload that.

    What advantage should this code have compared to vlookup ?

Posting Permissions

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