JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,531
- Office Version
- 365
- Platform
- Windows
Suppose I have a table like this one in a sheet.
Can I load it into a VBA Dictionary or Collection object so that I can search on the key (Product) and get back the value (Part#)?
Or do I need to do it in a loop one row at a time?
Thanks
Weighted Ratings.xlsm | ||||
---|---|---|---|---|
C | D | |||
7 | Product | Part# | ||
8 | A | K42-X3 | ||
9 | B | M21-44 | ||
10 | C | DL402 | ||
11 | D | MD3-902 | ||
12 | E | GT35 | ||
MrExcel |
Can I load it into a VBA Dictionary or Collection object so that I can search on the key (Product) and get back the value (Part#)?
Or do I need to do it in a loop one row at a time?
VBA Code:
Sub DictTest()
Const rnTable As String = "Table16"
Dim NumRows As Long
NumRows = Range(rnTable).Rows.Count
Debug.Print NumRows & " rows"
Dim DictTest As New Dictionary
Dim i As Long
For i = 1 To NumRows
DictTest.Add Range(rnTable).Cells(i, 1).Value, Range(rnTable).Cells(i, 2).Value
Next i
For i = 1 To NumRows
Debug.Print "Part# for product " & Range(rnTable).Cells(i, 1).Value _
& " is " & Range(rnTable).Cells(i, 2).Value
Next i
End Sub
Thanks