【问题标题】:Excel VBA: how to cast a generic control object into a ComboBox object?Excel VBA:如何将通用控件对象转换为 ComboBox 对象?
【发布时间】:2011-01-23 00:28:13
【问题描述】:

我需要将一个通用 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

我的问题是:我的表单中有大量组合框,我需要为所有组合框添加相同的选项。所以我想以编程方式做到这一点。

【问题讨论】:

  • 追随者说得对。您需要使用 Set。

标签: vba excel


【解决方案1】:

您可以使用 SET 来“转换”一个 VBA 对象,例如:

SET lbCurrentComboBox = ctlCurrent

【讨论】:

    【解决方案2】:

    如果phil's answer 不起作用,请尝试

    SET lbCurrentComboBox = ctlCurrent.Object
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多