Excel VBA:如何将通用控件对象转换为ComboBox对象? [英] Excel VBA: how to cast a generic control object into a ComboBox object?

查看:354
本文介绍了Excel VBA:如何将通用控件对象转换为ComboBox对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我的问题是:我的表单中有大量的ComboBox,我需要添加相同的所有的选择。所以我想做这个事情。

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.

推荐答案

您可以使用SET来投射一个VBA对象,例如:

You can "cast" an VBA Object by using SET, eg:

SET lbCurrentComboBox = ctlCurrent

这篇关于Excel VBA:如何将通用控件对象转换为ComboBox对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆