Dictionary - defining range

filido

New Member
Joined
Jun 7, 2019
Messages
21
Hi

I would like to add a list of items to a dictionary and then loop each item. The problem is, I don't know how to refer to a list range that is on another worksheet than the main data that is being filtered. "Items" are located on sheet wsList, so where am I suppose to add it?


Dim dict As Object, Items As Variant, c As Long, item As Variant
Set dict = CreateObject("Scripting.Dictionary")

With wsData


Dim ui As Double

If Range("D3").Value <> "" Then
ui = 2
Items = Range(.Range("A2"), .Cells(Rows.Count, "A").End(xlUp)) --> Items = Range(wsList.Range("A2"), .Cells(Rows.Count, "A").End(xlUp))doesn't work


For c = 1 To UBound(Items, 1)
dict(Items(c, 1)) = 1
Next

Else

item = Range("A2").Value --> ​same problem
ui = 1
End If

End with


The above is just a small part of my code, please let me know if you need to see more of it.
Many thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi,
The method is
[FONT=&quot]dict.[/FONT][FONT=&quot]Add[/FONT][FONT=&quot] [/FONT]key[FONT=&quot], [/FONT]item
 
Upvote 0
I think this is what you need:
Code:
Sub M2()

    Dim d   As Object: Set d = CreateObject("Scripting.Dictionary")
    Dim v   As Variant
    Dim x   As Long
    Dim i   As Long: i = 1
    
    With wsList
        x = Cells(.Rows.Count, 1).End(xlUp).Row - 1
        If Len(wsData.Cells(3, 4).Value) = 0 Then x = 1
        v = .Cells(2, 1).Resize(x).Value
    End With
    
    If x > 1 Then
        i = 2
        For x = LBound(v, 1) To UBound(v, 1)
            d(v(x, 1)) = 1
        Next x
    End If
            
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,547
Messages
6,120,139
Members
448,948
Latest member
spamiki

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