VBA通过listbox函数

我正在尝试通过将我的列表框对象传递给子函数来填充列表框,但每当我运行它,我得到一个types不匹配的错误。 我将MAIN.BoxY1对象传递给函数FillListBox,其中MAIN是列表框所在工作表的代码名称,BoxY1是我的列表框名称(ActiveX)。 当我改变FillListBox函数为每个实例包括MAIN.BoxY1而不是MyBox,它将正常工作。 将列表框传递给另一个函数的正确方法是什么?

Sub FillListBox(MyBox As ListBox, DataList As Variant) MyBox.MultiSelect = 1 For j = 1 To NumOutputs MyBox.AddItem DataList(j) Next j End Sub Sub BoxY1_Fill() FillListBox MAIN.BoxY1, TheData End Sub 

在Excel中有两种types的列表框:“内置”types和ActiveX版本。 根据您处理的types,您需要以不同的方式设置函数参数:

 Sub Testing() test1 Sheet1.ListBox1 ' <<ActiveX Listbox test2 Sheet1.ListBoxes("ListBox2") ' <<Forms Listbox test2 Sheet1.Shapes("ListBox2").OLEFormat.Object ' <<Forms Listbox (avoiding ' deprecated 'Listboxes') End Sub 'ActiveX listbox Function test1(lb As msforms.ListBox) Debug.Print lb.ListCount End Function 'Forms listbox Function test2(lb As ListBox) Debug.Print lb.ListCount End Function