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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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