找出OLEObject是否为ComboBox

时间:2013-06-07 10:10:21

标签: vba combobox

是否可以通过For Each循环找出OLEObject是否是ComboBox? 我在工作表上有几个Comboboxes,还有CommandButtons和其他什么。

Dim comboB As OLEObject
Dim ws As Worksheet: Set ws = ActiveSheet

For Each comboB in ws.OLEObjects
If typeName(comboB) = "ComboBox" Then
'Do something
End if
Next

问题是TypeName(stringvalue)仅适用于控件。 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

Sub sample()

   Dim comboB As Variant
   Dim ws As Worksheet
   Set ws = ActiveSheet

    With ws
        For Each comboB In .OLEObjects
            If TypeName(comboB.Object) = "ComboBox" Then
               MsgBox TypeName(comboB) & " is a combo box"
            End If
        Next

    End With
End Sub