简体   繁体   English

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

[英]Excel VBA: how to cast a generic control object into a ComboBox object?

I need to cast a generic VBA form control object into a ComboBox object so that I can add items to it. 我需要将一个通用的VBA表单控件对象转换为一个ComboBox对象,以便我可以向它添加项目。 The generic object won't allow me to insert items into my existing lists 通用对象不允许我将项目插入现有列表

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

My problem is: I have tons of ComboBoxes in my form and I need to add the same options for all of them. 我的问题是:我的表单中有大量的ComboBox,我需要为它们添加相同的选项。 So I would like to do this progrmatically. 所以我想这样做。

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

SET lbCurrentComboBox = ctlCurrent

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

SET lbCurrentComboBox = ctlCurrent.Object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM