I am exporting several INI-files from an Excel Spreadsheet, where one line means one INI-file, the INI-sections and keys are the columns. This works quite well so far, except it seems VBA is passing dictionaries ByRef (?), but let me explain:
The Excel Spreadsheet looks like this:
| section 1 | section 2 | section 3 | ...
| key1 | key2 | key3 | key4 | key3 | ...
name 1 | value | value | value | value | value | ...
name 2 | value | value | value | value | value | ...
...
I get all INI-sections, keys and values dynamically from spreadsheet, that's why I decided not to build a class module but to set up a dictionary structure with 3 nested dictionaries like this:
innerdict: holds key/value pairs of one section
middledict: holds section name and innerdict with key/value pairs
outerdict: holds line name with middledict
The following simplified code should do to explain my situation:
Set outerDict = New Scripting.Dictionary
Set middleDict = New Scripting.Dictionary
Set innerDict = New Scripting.Dictionary
For row = 3 to 10
For col = 1 to 5
key = Cells(a,b).Value
value = Cells(x,y).Value
innerdict.Add key, value
If nextSectionGroup Then
middledict.Add sectionName, innerdict
innerdict.RemoveAll
End If
Next col
outerdict.Add linename, middledict
middledict.RemoveAll
Next row
In words:
Starting in the first value row, I loop through columns, fill key/value pairs in the innerdict until I hit the end of a section.
Then I (want to) copy the innerdict
into the middledict
and after that delete the content of innerdict
so it can be used for the next section. If I hit the end of the entire row, I copy the middledict
into the outerdict
and delete the content of middledict
so it can be used for next row.
But after running the above code, the middledict
and innerdict
are not accessible via the outerdict
, it seems they are just empty. If I delete those .RemoveAll statements, the code works fine but fills the dicts with wrong content.
It seems to me, that dict.Add does not copy the content to the parent dict, but makes a reference. That would explain why content is empty after the .RemoveAll
.
But how to solve it, may someone have a hint please?
Thanks.