I am trying to understand syntax of:
Code:
Dict(Data(i, 1)) = Dict(Data(i, 1)) & Data(i, 2) & ";"
how to read the meaning here, for example what does "Dict(Data(i, 1))" and other syntax stand for?
Not sure how clearly I can explain, but I'll try using this sheet as a small example
Book1 |
---|
|
---|
| A | B |
---|
1 | Module | Processes |
---|
2 | A | P1 |
---|
3 | A | P2 |
---|
4 | A | P3 |
---|
5 | B | P9 |
---|
6 | C | P15 |
---|
7 | C | P16 |
---|
8 | | |
---|
|
---|
I'll also use the new version of that line of code as it changed slightly (putting the semicolon in a different place)
Code:
Dict(Data(i, 1)) = Dict(Data(i, 1)) & ";" & Data(i, 2)
Data is a 2-dimensional array holding the data you see above so still think of rows and columns.
The code line above is in a loop where the variable i runs from 1 to 7 as there are 7 rows of data.
When i = 1
Data(i,1) = Data(1,1) = "Module"
Data(i,2) = Data(1,2) = "Processes"
so the code line says
Dict("Module") =
Dict("Module") & ";" & "Processes"
Dict("Module") means what is stored in the dictionary for "Module". When this line is about to run for the first time nothing is in the dictionary at all so what is stored for "Module" is nothing & so this line is saying
In the dictionary for "Module" store whatever is already in there (nothing) and append a semicolon and append "Processes"
So, after processing row 1 above what is stored in the dictionary is what is shown in F2:G2 below
Row 2 above is similar in that "A" is not already in the dictionary so after processing row 2 what is in the dictionary is what is shown in F2:G3 below.
When we get to row 3 above "A" is repeated so our code line says, for the Key "A", store whatever is already in the dictionary for "A" (ie ";P1") and append a semicolon and whatever is in the 2nd column of that row. So for "A" now is stored ";P1;P2" as shown in row 4 below. Row 3 below is no longer relevant. In the end, there are just 4 Keys & 4 Items in the dictionary - the last row of each Key (coloured).
etc
Hope that helped. If you need more about dictionaries in vba,
here is one resource but Googling I'm sure would find you plenty more.
Book1 |
---|
|
---|
| A | B | C | D | E | F | G |
---|
1 | Module | Processes | | | After Row | Dict Keys | Dict Items |
---|
2 | A | P1 | | | 1 | Module | ;Processes |
---|
3 | A | P2 | | | 2 | A | ;P1 |
---|
4 | A | P3 | | | 3 | A | ;P1;P2 |
---|
5 | B | P9 | | | 4 | A | ;P1;P2;P3 |
---|
6 | C | P15 | | | 5 | B | ;P9 |
---|
7 | C | P16 | | | 6 | C | ;P15 |
---|
8 | | | | | 7 | C | ;P15;P16 |
---|
|
---|