开发者

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

开发者 https://www.devze.com 2022-12-21 14:51 出处:网络
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

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 frmIte开发者_运维技巧mInput.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.


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

SET lbCurrentComboBox = ctlCurrent


If phil's answer didn't work try

SET lbCurrentComboBox = ctlCurrent.Object
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号