簡體   English   中英

Excel VBA:如何將通用控件對象轉換為ComboBox對象?

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

我需要將一個通用的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

我的問題是:我的表單中有大量的ComboBox,我需要為它們添加相同的選項。 所以我想這樣做。

您可以使用SET“強制轉換”VBA對象,例如:

SET lbCurrentComboBox = ctlCurrent

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

SET lbCurrentComboBox = ctlCurrent.Object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM