I have a for loop with some conditions, I want to Create new Objects inside the for loop dynamically when met the conditions, i have commented the error in the code, Is there a way to accomplish this ? I am new to vba so xcuse my ignorant. Would appreciate any help.
Set rng1 = ThisWorkbook.Worksheets("Calculator").Range("M16:M30")
x = 1
For Each rcell In rng1.Cells
If Not IsEmpty(rcell) Then
If rcell = "1" Then
Set family_member_&x = CreateObject("Scripting.Dictionary") // ERROR HERE
family_member_&x.Add "family_group", "FG_1"
family_member_&x.Add "name", ThisWorkbook.Worksheets("Calculator").Range("B"&x)
family_member_&x.Add "date_of_birth", ThisWorkbook.Worksheets("Calculator").Range("C"&x)
ElseIf rcell = "2" Then
Set family_member_&x = CreateObject("Scripting.Dictionary")
family_member_&x.Add "family_group", "FG_2"
family_member_&x.Add "name", ThisWorkbook.Worksheets("Calculator").Range("B"&x)
family_member_&x.Add "relationship", ThisWorkbook.Worksheets("Calculator").Range("E"&x)
End If
x = x +1
End If
Next rcell
Scripting.Dictionary. So you could create a dictionary of dictionaries.Scripting.Dictionaryoutside your loop, then add new dictionaries to it within the loop using"family_member_" & xas the key.