从这个问题(How do I refer to a controls object, on a worksheet, using a variable name?开始,我尝试了以下实验,但没有成功。
如何访问Excel工作表上的控件?在哪里可以看到刚插入的控件的名称/属性?
答案 0 :(得分:4)
您可以执行以下操作:
Sub DropDown1_Change()
Dim s As Object
Set s = ActiveSheet.Shapes(Application.Caller)
Debug.Print s.ControlFormat.Value
End Sub
Application.Caller
包含形状名称,“包含”表单控件
类似地,您可以按名称访问其他控件:
Dim myName as String, c As Object
myName = "List Box 2"
Set c = ActiveSheet.Shapes(myName).ControlFormat
答案 1 :(得分:3)
我建议您创建一个Shape变量,而不是那样添加,以添加数据/属性。
类似this:
Sub t()
Dim newDD As Shape
Set newDD = ActiveSheet.Shapes.AddFormControl(xlDropDown, Left:=Cells(1, 1).Left, Top:=Cells(2, 1).Top, Width:=100, Height:=20)
With newDD
.ControlFormat.DropDownLines = 2
.ControlFormat.AddItem "Item 1", 1
.ControlFormat.AddItem "item 2", 2
.Name = "New Combo Box"
.OnAction = "myCombo_Change"
End With
End Sub
答案 2 :(得分:1)
试试
Dim checkBox1 As Object
Set checkBox1 = Sheet1.OLEObjects("CheckBox1").Object
MsgBox checkBox1.Value