scripting dictionary with 2 Keys and 1 Item

Gtabtr1

New Member
Joined
Feb 15, 2017
Messages
39
hello all,
Normally I would use Index/Match but I'm looking for a VBA solution.

In column A I have a list of numbers.
In column B I have a list of names.
In column C I have a date.

Can a scripting dictionary have 2 keys (in column A & B) and 1 item (in column C)? Match the value in columns A & B and retrieve date value in column C?

Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Depending on what you are trying to do, you could concatenate col A & B values then use that as the key
 
Upvote 0
Can a scripting dictionary have 2 keys (in column A & B) and 1 item (in column C)? Match the value in columns A & B and retrieve date value in column C?
Hi,
Sure, for example like this:
Rich (BB code):
Option Explicit
 
Dim Dic As Object
 
Sub InitDic()
  Dim a()
  Dim i As Long
  Dim key As String
  a() = Sheets(1).UsedRange.Range("A:C").Value  ' Change Sheets(1) to suit
  Set Dic = CreateObject("Scripting.Dictionary")
  With Dic
    .CompareMode = 1
    For i = 2 To UBound(a)  ' 2  is for skipping the 1st title row
      key = Trim(a(i, 1)) & vbTab & Trim(a(i, 2))
      If Len(key) > 0 Then .Item(key) = a(i, 3)
    Next
  End With
End Sub
 
Function GetDic(Val1, Val2)
  Dim key As String
  key = Trim(Val1) & vbTab & Trim(Val2)
  If Dic Is Nothing Then InitDic
  If Dic.Exists(key) Then
    GetDic = Dic(key)
  Else
    GetDic = vbNullString
  End If
End Function
 
Sub Test()
  MsgBox GetDic(Sheets(1).Range("A2"), Sheets(1).Range("B2"))
End Sub
Regards
 
Last edited:
Upvote 0
The correction: If Len(key) > 1 Then .Item(key) = a(i, 3)
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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