LOOKUP with VBA Dictionary & Arrays

nmmounir

New Member
Joined
Jul 26, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I was trying to use dictionary along with arrays to perform a lookup but it is giving a run time error 5 : Invalid procedure call or argument.

However, when I write the code using dictionary without arrays, it works perfectly. below are the two codes. What am i doing wrong with the dictionary arrays combo?

1704701223891.png


1704700880755.png


Dictionary array combo (not working & giving the above error)

1704701285777.png


Dictionary without Array (working perfectly)

1704701321386.png
 

Attachments

  • 1704701076634.png
    1704701076634.png
    27.5 KB · Views: 6

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Names is a varaint array so you need to use array address like this:
VBA Code:
If Mydic.Exists(Names(i, 1)) = True Then
You need to change it on the next line too
It is very helpful to post your code as text enclosed by the VBA formatting , which allows responder to Edit your code rather than typing it in a fresh,
 
Upvote 0
Names is a varaint array so you need to use array address like this:
VBA Code:
If Mydic.Exists(Names(i, 1)) = True Then
You need to change it on the next line too
It is very helpful to post your code as text enclosed by the VBA formatting , which allows responder to Edit your code rather than typing it in a fresh,
Thank you for your reply.

I wrote the code as you mentioned but i got a run time error 13 type mismatch ! any idea why? here is the code below as text.

Sub lookupwith_Dictionary()

Sheet2.Activate
Dim Mydic As Dictionary
Dim list As Variant
Dim Names As Variant, Countryname As Variant

list = Range("M2", Range("M2").End(xlDown).End(xlToRight)).Value
Set Mydic = New Dictionary

Dim i As Long
For i = 1 To UBound(list, 1)
Names = list(i, 1)
Countryname = list(i, 2)
If Mydic.Exists(Names) = False Then
Mydic.Add Names, Countryname
End If
Next i

Names = Range("P2", Range("P2").End(xlDown)).Value
For i = 2 To UBound(Names)
Names = Names(i, 1)
If Mydic.Exists(Names(i, 1)) = True Then
Range("Q" & i).Value = Mydic(Names(i, 2))
End If
Next i
 
Upvote 0
Names is a varaint array so you need to use array address like this:
VBA Code:
If Mydic.Exists(Names(i, 1)) = True Then
You need to change it on the next line too
It is very helpful to post your code as text enclosed by the VBA formatting , which allows responder to Edit your code rather than typing it in a fresh,
Please ignore the previous macro. it is now working as per below.

VBA Code:
Sub lookupwith_Dictionary()

Sheet2.Activate
Dim Mydic As Dictionary
Dim list As Variant
Dim Names As Variant, Countryname As Variant

list = Range("M2", Range("M2").End(xlDown).End(xlToRight)).Value
Set Mydic = New Dictionary

Dim i As Long
For i = 1 To UBound(list, 1)
    Names = list(i, 1)
    Countryname = list(i, 2)
    If Mydic.Exists(Names) = False Then
        Mydic.Add Names, Countryname
    End If
Next i

Dim row As Byte
row = 2
Names = Range("P2", Range("P2").End(xlDown)).Value
For i = LBound(Names) To UBound(Names)
    If Mydic.Exists(Names(i, 1)) = True Then
        Range("Q" & row).Value = Mydic(Names(i, 1))
        row = row + 1
    End If
Next i

End Sub
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in your last post for you this time. 😊
 
Upvote 0
Please ignore the previous macro. it is now working as per below.

VBA Code:
Sub lookupwith_Dictionary()

Sheet2.Activate
Dim Mydic As Dictionary
Dim list As Variant
Dim Names As Variant, Countryname As Variant

list = Range("M2", Range("M2").End(xlDown).End(xlToRight)).Value
Set Mydic = New Dictionary

Dim i As Long
For i = 1 To UBound(list, 1)
    Names = list(i, 1)
    Countryname = list(i, 2)
    If Mydic.Exists(Names) = False Then
        Mydic.Add Names, Countryname
    End If
Next i

Dim row As Byte
row = 2
Names = Range("P2", Range("P2").End(xlDown)).Value
For i = LBound(Names) To UBound(Names)
    If Mydic.Exists(Names(i, 1)) = True Then
        Range("Q" & row).Value = Mydic(Names(i, 1))
        row = row + 1
    End If
Next i

End Sub
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in your last post for you this time. 😊
Thank you Peter. May you please how to use these code tags? I am not sure how you did this?
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags in your last post for you this time. 😊
Ok I will look it up and follow it.
 
Upvote 0
Try:
VBA Code:
Sub FillCountry()
    Application.ScreenUpdating = False
    Dim v1 As Variant, v2 As Variant, dic As Object, i As Long, x As Long, arr() As Variant, lRow As Long, Val As String
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v1 = Range("M2", Range("M" & Rows.Count).End(xlUp)).Resize(, 2).Value
    v2 = Range("P2", Range("P" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        Val = v1(i, 1)
        If Not dic.exists(Val) Then
            dic.Add Val, v1(i, 2)
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            Val = v2(i, 1)
            x = x + 1
            ReDim Preserve arr(1 To x)
            arr(x) = dic(Val)
        End If
    Next i
    Range("Q2").Resize(lRow - 1) = Application.Transpose(arr)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try:
VBA Code:
Sub FillCountry()
    Application.ScreenUpdating = False
    Dim v1 As Variant, v2 As Variant, dic As Object, i As Long, x As Long, arr() As Variant, lRow As Long, Val As String
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v1 = Range("M2", Range("M" & Rows.Count).End(xlUp)).Resize(, 2).Value
    v2 = Range("P2", Range("P" & Rows.Count).End(xlUp)).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v1) To UBound(v1)
        Val = v1(i, 1)
        If Not dic.exists(Val) Then
            dic.Add Val, v1(i, 2)
        End If
    Next i
    For i = LBound(v2) To UBound(v2)
        If dic.exists(v2(i, 1)) Then
            Val = v2(i, 1)
            x = x + 1
            ReDim Preserve arr(1 To x)
            arr(x) = dic(Val)
        End If
    Next i
    Range("Q2").Resize(lRow - 1) = Application.Transpose(arr)
    Application.ScreenUpdating = True
End Sub
This a great code. just one question as I am new to VBA Dictionary.
If dic.exists(v2(i, 1)) Then
Val = v2(i, 1)
arr(x) = dic(Val)
shouldn't Val = v2(i,2) ? since it is the second column where the country list is?
 
Upvote 0
VBA Code:
Val
refers to the key in the dictionary, which is the value of v2(i,1) and
VBA Code:
dic(Val)
refers to the item in the dictionary, which is the value of v1(i,2).
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top