ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Iterating over a nested Dictionary object (https://www.excelbanter.com/excel-programming/418652-iterating-over-nested-dictionary-object.html)

donvreug

Iterating over a nested Dictionary object
 
Hi,

I've setup a nested Dictionary object using classes that initialise
nested dictionary objects. This appears to build ok, however when I
try and iterate over this nested object I get the following debug
message:

"Run-time error '424', Object required"

My code snippet is as follows;

For i = 1 to MDict.Count
For j = 1 to MDict.Items(i).subDict1.Count
For k = 1 to MDict.Items(i).subDict1.Items(j).subDict2.Count
...
Next k
Next j
Next i

The run-time error occurs on the For j = 1... line.

I'm not sure the syntax is correct as this is my first attempt at
using Dictionaries.

I also tried the following with the same result.

For Each md In MDict.items
For Each sd1 In MDict.Item("subDict1").Items
For ach sd2 In MDict.Item("subDict1").Item("subDict2").Items
...
Next
Next
Next

Any help is greatly appreciated.

Jim Cone[_2_]

Iterating over a nested Dictionary object
 
A Dictionary object is zero based.
Your loops should start at zero and end at one less than the count...
For i = 0 to MDict.Count - 1
--
Jim Cone
Portland, Oregon USA



"donvreug"

wrote in message
Hi,
I've setup a nested Dictionary object using classes that initialise
nested dictionary objects. This appears to build ok, however when I
try and iterate over this nested object I get the following debug
message:
"Run-time error '424', Object required"

My code snippet is as follows;
For i = 1 to MDict.Count
For j = 1 to MDict.Items(i).subDict1.Count
For k = 1 to MDict.Items(i).subDict1.Items(j).subDict2.Count
...
Next k
Next j
Next i

The run-time error occurs on the For j = 1... line.
I'm not sure the syntax is correct as this is my first attempt at
using Dictionaries.

I also tried the following with the same result.
For Each md In MDict.items
For Each sd1 In MDict.Item("subDict1").Items
For ach sd2 In MDict.Item("subDict1").Item("subDict2").Items
...
Next
Next
Next

Any help is greatly appreciated.

donvreug

Iterating over a nested Dictionary object
 
Thanks for spotting that Jim.

The original problem remains however.

Any other thoughts?

Regards

Don

On Oct 17, 11:25*pm, "Jim Cone" wrote:
A Dictionary object is zero based.
Your loops should start at zero and end at one less than the count...
For i = 0 to MDict.Count - 1
--
Jim Cone
Portland, Oregon *USA



Jim Cone[_2_]

Iterating over a nested Dictionary object
 
Don,
'Assigns value returned by the Items method to a variant array
'and iterates thru that...

Dim vItems As Variant
Dim vTemp As Variant
'variant array contains all sub dictionaries
vItems = mDict.Items

For i = 0 To UBound(vItems)
'variant array contains all items in subdictionary
vTemp = vItems(i).Items
For j = 0 To UBound(vTemp)
'Assumes, each item in the subdictionary
'contains a range object
For k = 1 To vTemp(j).Count
MsgBox vTemp(j)(k).Value
Next 'k
Next 'j
Next 'i
--
Jim Cone
Portland, Oregon USA


"donvreug"
wrote in message
Thanks for spotting that Jim.
The original problem remains however.
Any other thoughts?
Regards
Don

On Oct 17, 11:25 pm, "Jim Cone" wrote:
A Dictionary object is zero based.
Your loops should start at zero and end at one less than the count...
For i = 0 to MDict.Count - 1
--
Jim Cone
Portland, Oregon USA



donvreug

Iterating over a nested Dictionary object
 
Thanks Jim, much appreciated.

Regards

Don


All times are GMT +1. The time now is 02:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com