VBA Excel-如何访问工作表框架内的控件?重构/优化
使用Excel 2010 我有一个包含三个ActiveX框架的工作表。每个框架都包含两个或多个OptionButton。工作表上的重置按钮将optionbuttons的值重置为“False” 现在,我可以为每个帧使用单独的For循环来重置它们:VBA Excel-如何访问工作表框架内的控件?重构/优化,vba,excel,activex,Vba,Excel,Activex,使用Excel 2010 我有一个包含三个ActiveX框架的工作表。每个框架都包含两个或多个OptionButton。工作表上的重置按钮将optionbuttons的值重置为“False” 现在,我可以为每个帧使用单独的For循环来重置它们: Private Sub CommandButtonReset_Click() 'This button resets all the OptionButtons to False (unchecked) Dim x As Control For E
Private Sub CommandButtonReset_Click()
'This button resets all the OptionButtons to False (unchecked)
Dim x As Control
For Each x In Frame1.Controls
x.Value = False
Next
For Each x In Frame2.Controls
x.Value = False
Next
For Each x In Frame3.Controls
x.Value = False
Next
End Sub
…但我想使用一个嵌套For循环来重置它们,如下所示:
Dim xControl as control
Dim xFrame as Frame
For Each xFrame in (ActiveSheet.Frames? .Shapes? .OLEObjects?)
For Each xControl in xFrame
xControl.Value = False
Next
Next
Dim xControl作为控制
将X帧作为帧进行调整
对于(ActiveSheet.Frames?.Shapes?.OLEObjects?)中的每个xFrame
对于xFrame中的每个xControl
xControl.Value=False
下一个
下一个
在网上和书籍中进行了大量搜索后,我找不到访问活动工作表中每个ActiveX框架的正确方法。请尝试以下方法:
Sub Tester()
Dim o As OLEObject, c
For Each o In Sheet1.OLEObjects
'is this a Frame?
If TypeName(o.Object) = "Frame" Then
For Each c In o.Object.Controls
'is this a checkbox?
If TypeName(c) = "CheckBox" Then
c.Value = False
End If
Next
End If
Next o
End Sub
录制一个宏,选择其中一个帧并更改某些内容,然后查看宏源。如果可以在代码中完成,您将看到如何在代码中选择帧。