Excel VBA:如何将通用控件对象转换为ComboBox对象?

Mar*_*que 10 excel vba excel-vba

我需要将一个通用的VBA表单控件对象转换为一个ComboBox对象,以便我可以向它添加项目.通用对象不允许我将项目插入现有列表

Dim ctlCurrent As MSForms.Control
For Each ctlCurrent In frmItemInput.Controls
    If TypeName(ctlCurrent) = "ComboBox" Then
         Dim lbCurrentComboBox As MSForms.ComboBox
         lbCurrentComboBox = ctlCurrent 'This is where the error occurs, when I try to convert one into another

         ' Adiciona os itens necessários
         lbCurrentComboBox.AddItem ("R")
         lbCurrentComboBox.AddItem ("D")
    End If
Next ctlCurrent
Run Code Online (Sandbox Code Playgroud)

我的问题是:我的表单中有大量的ComboBox,我需要为它们添加相同的选项.所以我想这样做.

Phi*_*off 15

您可以使用SET"强制转换"VBA对象,例如:

SET lbCurrentComboBox = ctlCurrent
Run Code Online (Sandbox Code Playgroud)


小智 8

如果phil的答案不起作用试试

SET lbCurrentComboBox = ctlCurrent.Object
Run Code Online (Sandbox Code Playgroud)