Dictionary buggy for objects ?

dpenny

New Member
Joined
Jan 27, 2005
Messages
29
I am trying to store a range into a Dictionary and then retrieve by key. I add by
dict.add key:=c.value, item:=c
where c is the variable in a For Each ... over a range. Eventually I want the "item" to be a Union of cell ranges. But in the meantime, it does not seem to be storing objects as the item.

when I am done and test IsObject in Immediate Window

isobject( dict.items(1) ) gives True, but
isobject( dict.item("IMG") ) gives False even though IMG is a valid key.

any ideas what's going wrong?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The syntax might be wrong:
Code:
isobject( dict.item("IMG") )

Try instead:
Code:
isobject( dict.item[B][COLOR="Red"]s[/COLOR][/B]("IMG") )
 
Upvote 0
Don't think so:

dict.items..... takes an integer argument

dict.item...... takes a string as hash key argument
 
Upvote 0
That seems like a convoluted way to union a range.

Code:
Sub x()
    Dim dic         As Scripting.Dictionary
    Dim cell        As Range
    Dim vKey        As Variant
 
    Set dic = New Dictionary
 
    With Range("A1:C10")
        .Formula = "=randbetween(1,5)"
        .Value = .Value
        For Each cell In .Cells
            If cell.Value = 1 Then dic.Add Key:=cell.Address, Item:=cell
        Next cell
    End With
 
    For Each vKey In dic.Keys
        Debug.Print vKey, dic.Item(vKey).Address, dic.Item(vKey).Value
    Next vKey
End Sub
 
Upvote 0
This is interesting:

Early binding succeeds:
Code:
Sub foo1()
Dim d As [COLOR="Blue"]Dictionary[/COLOR]
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "IMG", Sheet1.Cells(1, 1)
    Debug.Print IsObject(d.Item("IMG"))
    Debug.Print IsObject(d.Items(0))
End Sub

Late binding fails:
Code:
Sub foo2()
Dim d As [COLOR="Blue"]Object[/COLOR]
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "IMG", Sheet1.Cells(1, 1)
    Debug.Print IsObject(d.Item("IMG"))
    Debug.Print IsObject(d.Items(0))
End Sub

This is a workaround for the case of early binding:
Code:
Sub foo3()
Dim d As Object
Dim a
    
    Set d = CreateObject("Scripting.Dictionary")
    d.Add "IMG", Sheet1.Cells(1, 1)
    Debug.Print IsObject(d.Item("IMG"))
    a = d.Items
    Debug.Print IsObject(a(0))

End Sub

I think I'd be more interested in what you want to do with this though. You are adding ranges to your dictionary so you already know you have objects.
 
Upvote 0
THANKS to all for your thoughtful replies.

Couldn't identify the root problem unless it was early vs. late binding, so instead of storing objects I stored a concatenation of the addresses of the ranges (as a string) and then split them apart when I needed to use/read them.

Thx again.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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