简体   繁体   中英

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. 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. So I would like to do this progrmatically.

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

SET lbCurrentComboBox = ctlCurrent

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

SET lbCurrentComboBox = ctlCurrent.Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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