PDA

View Full Version : [SOLVED:] Help with syntax for nested dictionaries



EirikDaude
08-12-2014, 02:19 AM
I have the following code, which stops at the line

Debug.Print (CStr(key1) + " - " + CStr(stoppForVeke(key1)) + " - " + CStr(stoppForVeke(key1)(key2)))
with the error "Run-time error '450': Wrong number of arguments or invalid property assignment.

I suspect the problem is how I refer to the "innermost" dictionary, though I thought the syntax I was using is correct? Can someone please explain to me what I am doing wrong? Is there some other (and better) way to referring to that dictionary?


Option Explicit

Sub finnverdier()
' Oppretting av variabler
Dim områdeÅSøkeI As Range, c As Range
Dim vekenr As Long, stanstid As Long, maskin As String
Dim key1 As Variant, key2 As Variant
Dim stoppForVeke As Dictionary, stoppForMaskin As Dictionary
Set stoppForVeke = New Dictionary

' Ferdig med initialisering
With Innlimt
Set områdeÅSøkeI = Range(.Range("C3"), .Range("C1048576").End(xlUp))
If Not Intersect(.Range("C2"), områdeÅSøkeI) Is Nothing Then
Exit Sub
End If
End With

For Each c In områdeÅSøkeI
vekenr = c.Offset(0, -2): stanstid = c.Offset(0, 2): maskin = c

If stoppForVeke.Exists(vekenr) Then ' Treng ikkje å lage ny ordbok for veka, legg berre til den gamle
If stoppForVeke(vekenr).Exists(maskin) Then ' Treng berre å legge til tida for den eksisterende maskina
stoppForVeke(vekenr)(maskin) = stoppForVeke(vekenr)(maskin) + stanstid
Else ' Må opprette legge til både element og tid for den eksisterande veka
stoppForVeke(vekenr).Add maskin, stanstid
End If
Else ' Må lage nye ordbøker for maskina
Set stoppForMaskin = New Dictionary
stoppForMaskin.Add maskin, stanstid
stoppForVeke.Add vekenr, stoppForMaskin
End If
Next

For Each key1 In stoppForVeke
For Each key2 In stoppForVeke(key1)
Debug.Print (CStr(key1) + " - " + CStr(stoppForVeke(key1)) + " - " + CStr(stoppForVeke(key1)(key2)))
Next
Next
End Sub

snb
08-12-2014, 02:47 AM
For j=1 to stoppForVeke.count
Debug.Print j & " - " & stoppForVeke.keys()(j)
Next


You could have known....

http://www.snb-vba.eu/VBA_Dictionary_en.html#L_15

EirikDaude
08-13-2014, 03:08 AM
Thanks for your reply. However trying trying to replace

For Each key1 In stoppForVeke
For Each key2 In stoppForVeke(key1)
Debug.Print (CStr(key1) + " - " + CStr(stoppForVeke(key1)) + " - " + CStr(stoppForVeke(key1)(key2)))
Next
Next
with

For j=1 To stoppForVeke.count
Debug.Print j & " - " & stoppForVeke.keys()(j)
Next
gives me a Run-time error '9': Subscript out of range.

And yeah I could probably have read a bit more on the subject and did a bit more searching before I came here. I'm not entirely sure what it is I am to take away from lesson 15 on your webpage though. Has it to do with how you describe putting all the variables into a string/variable? Cause I can't really see a strong similarity between what you are doing there and the code example you provided above. I am not a very proficient VBA coder though, so it could just be a lack of understanding the code :)

Anyway, looking at the code again today, I see that I was simply a derp yesterday, and tried printing the actual dictionary object. Changing the print-line to

Debug.Print (CStr(key1) + " - " + CStr(key2) + " - " + CStr(stoppForVeke(key1)(key2)))
fixed everything.

snb
08-13-2014, 04:03 AM
Rather obvious


For j=1 To stoppForVeke.count
Debug.Print j & " - " & stoppForVeke.keys()(j-1)
Next

EirikDaude
08-13-2014, 04:21 AM
It is :)

The output from the two for loops is still quite dissimilar though?

49 - Tankrensk - 120
51 - Venting SAG - 48
53 - Venting Ovn - 600
1 - Tankrensk - 60
3 - SIR - 330
5 - Vogner - 45
5 - Venting Ovn - 150
5 - Tankrensk - 60
5 - Venting Sag - 120
7 - Venting Ovn - 120
7 - Former - 60
7 - Venting SAG - 45
7 - CF - 180
7 - Tankrensk - 210
8 - Former - 40
8 - Venting Ovn - 60
9 - Tilrigging - 90
9 - Venting Ovn - 63
10 - Venting Ovn - 30
10 - Former - 20
13 - Tilrigging - 120
15 - Venting Ovn - 60
16 - Venting SAG - 110
16 - Venting Sag - 40
17 - Venting Ovn - 200
17 - Omrørar - 60
17 - SIR - 180
18 - Utrenning - 120
18 - Venting Sag - 180
18 - Venting SAG - 60
19 - Venting SIR - 40
20 - Støypebord - 450
21 - Former - 200
21 - Utrenning - 240
21 - Støypebord - 120
21 - Tankrensk - 400
21 - Kran - 1200
21 - Venting sag - 120
21 - Venting sir - 40
21 - Venting SAG - 180
21 - Tilrigging - 240
23 - Venting Ovn - 500
23 - Støypebord - 180
23 - Former - 30
24 - Støypebord - 720
25 - Støypebord - 840
26 - Venting SAG - 80
26 - Venting SIR - 480
27 - Kran - 565
27 - Støypebord - 230
27 - Venting Ovn - 240
27 - Renner - 180
27 - Vogner - 330
27 - Utrenning - 330
28 - Støypebord - 60
28 - Venting Ovn - 120
29 - Venting Ovn - 240
30 - Venting Ovn - 100
30 - Venting Sag - 120
30 - Støypebord - 20
31 - Venting SAG - 240
31 - Venting Sag - 80
31 - Manglande forbruksmateriell - 20
32 - Venting Sag - 30
32 - Venting SAG - 240


1 - 49
2 - 51
3 - 53
4 - 1
5 - 3
6 - 5
7 - 7
8 - 8
9 - 9
10 - 10
11 - 13
12 - 15
13 - 16
14 - 17
15 - 18
16 - 19
17 - 20
18 - 21
19 - 23
20 - 24
21 - 25
22 - 26
23 - 27
24 - 28
25 - 29
26 - 30
27 - 31
28 - 32

snb
08-13-2014, 04:52 AM
I'm not a clairvoyant.....

Without a smple file....

EirikDaude
08-13-2014, 05:20 AM
I figured the syntax for outputting the contents of the dictionaries to the Immediate window would be the same no matter the contents they collected from the worksheet, and that I therefore didn't need to create one.

snb
08-13-2014, 06:02 AM
No need to make helpers comfortable....

EirikDaude
08-13-2014, 06:51 AM
Sorry, maybe I read your previous post as more snippy than it was. I really appreciate the help you and a lot of other forum members provide here.