Using dictionary in different userform

easyhoting

New Member
Joined
Jun 25, 2012
Messages
4
Hello all,

Does anyone know how to passover a dictionary from one userform to the next userform? In the program I am trying to pass on the dictionary generated in the first userform to the second userform. on initialization of the second userform, the keys from the dictionary will be populated and will be added to a combobox of the second userform. When I run this program, the dictionary was successfully created in the first userform, but it cannot be passed on to the second userform and the dictionary would show up empty. I am wondering what can be done so that the values stored in dictionary can be retained for the second userform.

Thanks

Code:
Dim dict
Dim dict_keys

'First userform
Private Sub CommandButton1_Click()
    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add 1.01, Array("a", "b", "c", "d")
    dict.Add 1.02, Array("e", "f", "g", "h")
    UserForm2.Show
End Sub


'Second userform
Private Sub userform_Initialize()
    dict_keys = dict.keys
    For i = 0 To dict_keys.Count - 1
        ComboBox1.AddItem (dict_keys(i))
    Next
End Sub
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
This worked for Me
Declare the Variable "Dict" as "Public at top of basic Module.
Code:
Public dict
The code for userform2 "Initialize" as below:-
NB:- I placed a CommandButton on userform2 (as below)to show the item (2) from each Dictionary Key, thought it might be helpful !!!
Code:
Private Sub CommandButton1_Click()
MsgBox dict.Item(Val(Me.ComboBox1))(2)
End Sub
Private Sub userform_Initialize()
   ComboBox1.List = dict.keys
End Sub
 

easyhoting

New Member
Joined
Jun 25, 2012
Messages
4
This worked for Me
Declare the Variable "Dict" as "Public at top of basic Module.
Code:
Public dict
The code for userform2 "Initialize" as below:-
NB:- I placed a CommandButton on userform2 (as below)to show the item (2) from each Dictionary Key, thought it might be helpful !!!
Code:
Private Sub CommandButton1_Click()
MsgBox dict.Item(Val(Me.ComboBox1))(2)
End Sub
Private Sub userform_Initialize()
   ComboBox1.List = dict.keys
End Sub


Thanks a lot, that solved my problem
 

Watch MrExcel Video

Forum statistics

Threads
1,123,489
Messages
5,601,986
Members
414,489
Latest member
Xlambda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top