Excel VBA:如何将通用控件对象转换为 ComboBox 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2368252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA: how to cast a generic control object into a ComboBox object?
提问by Marcos Buarque
I need to cast a generic VBA form control object into a ComboBox object so that I can add items to it. The generic object won't allow me to insert items into my existing lists
我需要将通用 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
My problem is: I have tons of ComboBoxes in my form and I need to add the same options for all of them. So I would like to do this progrmatically.
我的问题是:我的表单中有大量 ComboBox,我需要为所有这些选项添加相同的选项。所以我想以编程方式做到这一点。
回答by Phil Rykoff
You can "cast" an VBA Object by using SET, eg:
您可以使用 SET 来“投射”一个 VBA 对象,例如:
SET lbCurrentComboBox = ctlCurrent

