Scripting Dictionary Byref Type Mismatch Error

avd88

Board Regular
Joined
Jan 18, 2016
Messages
112
Hi,

Trying to loop through key-value pairs in a data dictionary and use those values in a sub I'm calling but keep getting the error Byref Type Mismatch. Can't figure out what the problem is.
Any ideas?

This is the sub I'm calling:

VBA Code:
Sub DropDown(o As MSForms.ComboBox, r As String)
Dim I As Worksheet
    Set I = Worksheets("Inputs")
   
    o.List = I.Range(r).Value
End Sub

The sub works perfectly when implemented like this:

Code:
'DJOHS DROPDOWN'
    Call DropDown(SDJOHSComboBox, "DJOHS")
    Call DropDown(LDJOHSComboBox, "DJOHS")

What I'm trying to do is create a data dictionary with all the key - item pairs and loop through that sub. Something like this:

Code:
Dim iDict As New Scripting.dictionary
   
    'Create a Dictionary Object
    iDict.Add "SStageComboBox", "Stage"
    iDict.Add "SYearComboBox", "Year"
    iDict.Add "LYearComboBox", "Year"
    iDict.Add "SPeriodComboBox", "Period"
    iDict.Add "LPeriodComboBox", "Period"
   
    'Loop Thru each key item pair
    For Each I In iDict.Keys
        Call DropDown(iDict.Keys(I), iDict.Items(I))
    Next I

The error is flagging this portion of the code:
Code:
iDict.Keys(I)

Tried removing the "" on the dictionary keys as those are used as a ComboBox object in the sub but that didn't really work.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Syntax? Without testing, does this work?
VBA Code:
For Each I In iDict.Keys
        Call DropDown(I, iDict(I))
Next I
 
Upvote 0
Hi,
try passing your arguments ByVal & see if this resolves your issue

Rich (BB code):
Sub DropDown(ByVal o As MSForms.ComboBox, ByVal r As String)
Dim I As Worksheet
    Set I = Worksheets("Inputs")
  
    o.List = I.Range(r).Value
End Sub

Dave
 
Upvote 0
Tried the suggestions separately and it did not work, but a combination of the two of them worked perfectly! TEAMWORK!!!

Thank you very much friends!
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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
Back
Top