我已经退出了很多ComboBox对象,我试图通过循环来显示ComboBox的值:
For i=1 to 100
MsgBox ("ComboBox" & i & ".Text")
Next
当然这段代码由于引号而无效,但是当我删除它们时,我有一个编译错误。
我该怎么做?
答案 0 :(得分:3)
OP不会说明相关对象是ActiveX
还是Form Control
个对象。
要处理这两种对象类型,如果对象名称不是ComboBox*
,请尝试使用
Sub Demo()
Dim ws As Worksheet
Dim shp As Shape
Dim cb As ComboBox
Set ws = ActiveSheet
For Each shp In ws.Shapes
With shp
Select Case .Type
Case msoFormControl
If .FormControlType = xlDropDown Then
If .ControlFormat.Value = 0 Then
MsgBox .Name & " = "
Else
MsgBox .Name & " = " & .ControlFormat.List(.ControlFormat.Value)
End If
End If
Case msoOLEControlObject
If .OLEFormat.progID = "Forms.ComboBox.1" Then
Set cb = .OLEFormat.Object.Object
MsgBox cb.Name & " = " & cb.Value
End If
End Select
End With
Next
End Sub
答案 1 :(得分:1)
这对我有用。假设您的组合框的名称确实是ComboBox1
到ComboBox100
。
Dim cb As ComboBox
Dim i As Long
For i = 1 To 100
Set cb = Sheet1.Shapes("ComboBox" & i).OLEFormat.Object.Object ' Ouch!
MsgBox cb.Text
Next i
我从here获得的.Object.Object
技巧。