Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around)
Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Hi Julio,
What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Rob,
I thought that the Keys method returned a variant array and that the only way to return a key was by using the following??? Dim S as Variant dic.Add "foo", "bar" S = dic.Keys MsgBox S(0) Regards, Jim Cone San Francisco, CA "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
thanks much
"Rob Bovey" wrote: Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Rob,
Help gives all its dictionary examples using late binding, and this works Dim dic As Object Dim s As String Dim a Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" a = dic.keys s = a(0) Weird or what? -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Jim,
That is the same results as I found, but it's not the same as the OP and Rob tried. They tried to access the keys array directly, and failed. This is so even if you declare s as variant in the OP's code. Whereas Rob accessed in directly with early binding. Still not clear why you cannot directly access the keys array with late binding. -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Jim Cone" wrote in message ... Rob, I thought that the Keys method returned a variant array and that the only way to return a key was by using the following??? Dim S as Variant dic.Add "foo", "bar" S = dic.Keys MsgBox S(0) Regards, Jim Cone San Francisco, CA "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Bob,
it didn't work for me. Not unless i followed Rob's suggestion.. "Bob Phillips" wrote: Rob, Help gives all its dictionary examples using late binding, and this works Dim dic As Object Dim s As String Dim a Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" a = dic.keys s = a(0) Weird or what? -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Julio,
That's odd. Just tried it again, and this code still works for me Sub Macro1() Dim dic As Object Dim s As Variant Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" Dim a a = dic.keys MsgBox a(0) End Sub -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "julio" wrote in message ... Bob, it didn't work for me. Not unless i followed Rob's suggestion.. "Bob Phillips" wrote: Rob, Help gives all its dictionary examples using late binding, and this works Dim dic As Object Dim s As String Dim a Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" a = dic.keys s = a(0) Weird or what? -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Hi Bob,
Yeah that is really strange. I can't imagine what's going on under the hood there. -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "Bob Phillips" wrote in message ... Rob, Help gives all its dictionary examples using late binding, and this works Dim dic As Object Dim s As String Dim a Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" a = dic.keys s = a(0) Weird or what? -- HTH Bob Phillips ... looking out across Poole Harbour to the Purbecks (remove nothere from the email address if mailing direct) "Rob Bovey" wrote in message ... Hi Julio, What you have ought to work in theory, but I get the same error you do. It seems like the Dictionary object doesn't like late binding. If I set a reference to the Microsoft Scripting Runtime and change the code to use early binding as shown below it works as expected. Sub Macro1() Dim dic As Scripting.Dictionary Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.Keys(0) End Sub -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "julio" wrote in message ... How do you access dictionary keys? What's wrong with this? (I get runtime error "let not defined, get doesn't return object" or the other way around) Sub Macro1() Dim dic As Object Dim s As String Set dic = CreateObject("Scripting.Dictionary") dic.Add "foo", "bar" s = dic.keys(0) End Sub |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Hi Rob,
Yeah that is really strange. I can't imagine what's going on under the hood there. I guess there's an optional parameter on the Keys property, so we can change an item's Key value: Public Property Let Keys(Optional vOldKey As Variant, vNewKey As Variant) End Property Public Property Get Keys(Optional vOldKey As Variant) If Not IsMissing(vOldKey) Then Err.Raise "Not Implemented" End If End Property So when late-bound, the interpreter is passing the index to the property (which gives an error from within the property), but when early-bound I think there's a check that the compiler can do to see if a property is implemented, so it sees that the Keys Property Get doesn't in fact implement a parameter, so knows that the (0) is an index into the array returned by the Keys() property. Regards Stephen Bullen Microsoft MVP - Excel www.BMSLtd.ie |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Hi Stephen,
I don't know. Keys is a method not a property, and it's documented to return an array of Keys, so the syntax Dictionary.Keys(Index) should work in either case, returning the Index item from the array. Why it pukes when used directly that way with late-binding but has no problem assigning its array to a Variant still doesn't make sense to me. -- Rob Bovey, MCSE, MCSD, Excel MVP Application Professionals http://www.appspro.com/ * Please post all replies to this newsgroup * * I delete all unsolicited e-mail responses * "Stephen Bullen" wrote in message ... Hi Rob, Yeah that is really strange. I can't imagine what's going on under the hood there. I guess there's an optional parameter on the Keys property, so we can change an item's Key value: Public Property Let Keys(Optional vOldKey As Variant, vNewKey As Variant) End Property Public Property Get Keys(Optional vOldKey As Variant) If Not IsMissing(vOldKey) Then Err.Raise "Not Implemented" End If End Property So when late-bound, the interpreter is passing the index to the property (which gives an error from within the property), but when early-bound I think there's a check that the compiler can do to see if a property is implemented, so it sees that the Keys Property Get doesn't in fact implement a parameter, so knows that the (0) is an index into the array returned by the Keys() property. Regards Stephen Bullen Microsoft MVP - Excel www.BMSLtd.ie |
#12
Posted to microsoft.public.excel.programming
|
|||
|
|||
dictionary keys
Hi Rob,
I don't know. Keys is a method not a property, and it's documented to return an array of Keys, so the syntax Dictionary.Keys(Index) should work in either case, returning the Index item from the array. Why it pukes when used directly that way with late-binding but has no problem assigning its array to a Variant still doesn't make sense to me. D'Oh! So it is - I was combining the Key and Keys and coming up with 6! Regards Stephen Bullen Microsoft MVP - Excel www.BMSLtd.ie |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Customizing a dictionary | Excel Worksheet Functions | |||
How to remove a word from dictionary? | Excel Discussion (Misc queries) | |||
dictionary function | Excel Programming | |||
Dictionary Functions | Excel Programming | |||
Dictionary objet | Excel Programming |