我正在运行一个子程序,我需要计算一个组框中已选中的复选框的数量,并对几个组进行此操作。 编辑:我忘了提及我使用的是窗体控件,而不是ActiveX控件。
我的第一个问题是创建一组组框。我尝试使用
GB_Array = Activesheet.Shapes.Range(Array(Cells(x, y), Cells(z, y))) ' x,y,z defined elsewhere
我可以通过手动添加来完成这项工作,但这并不理想。我的第二个问题与这部分有关:
Option Base 1
Dim cbox as Checkbox
Dim C_cbox as Integer
GB_Array = Array("Name1", "Name2") ' Manually adding groupboxes to the array
For i = 1 to Ubound(GB_Array, 1)
For Each cBox In Activesheet.Shapes.Range(GB_Array(1))
If cBox.Checked = True Then
C_cbox = C_cbox + 1
End If
Next cBox
Next i
返回类型不匹配错误13。 编辑:似乎我犯了用复选框对分组框进行分组的错误,答案适用于“ ugnrouped”分组框(所以我可以不使用复选框移动分组框)。
答案 0 :(得分:1)
这是您要尝试的吗?
我的假设:所有控件都是表单控件。
我已注释了该代码,因此您在理解它时应该没有问题。仍然,如果您有任何疑问,只需询问:)
Sub Sample()
Dim ws As Worksheet
Dim gbox As GroupBox
Dim Shp As Shape
Dim rngGBox As Range
Dim C_cbox As Integer
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Loop through group boxes
For Each gbox In .GroupBoxes
'~~> Get the range of the groupbox
Set rngGBox = .Range(gbox.TopLeftCell, gbox.BottomRightCell)
'~~> Loop through all shapes
For Each Shp In gbox.Parent.Shapes
If Shp.Type = msoFormControl Then
'~~> Check if the shape is within the groupbox range
If Not Intersect(Shp.TopLeftCell, rngGBox) Is Nothing Then
If Not Shp Is gbox Then
'~~> Check if it is a checkbox
If Shp.FormControlType = xlCheckBox Then
'~~> Check if it is checked
If Shp.ControlFormat.Value = xlOn Then
C_cbox = C_cbox + 1
End If
End If
End If
End If
End If
Next Shp
Next gbox
End With
End Sub
如果您要使用特定的组框,则可以使用此
Sub Sample()
Dim ws As Worksheet
Dim grpBxNames As String
Dim grpBxArray As Variant
Dim gbox As GroupBox
Dim Shp As Shape
Dim rngGBox As Range
Dim C_cbox As Integer
'~~> Change this to the relevant sheet
Set ws = Sheet1
'~~> Put the names separated by comma
'~~> we will create the array during runtime
grpBxNames = "Group Box 1,Group Box 6"
grpBxArray = Split(grpBxNames, ",")
With ws
'~~> Loop through array of group boxes
For i = 1 To UBound(grpBxArray)
'~~> Set you object
Set gbox = .GroupBoxes(grpBxArray(i))
'~~> Get the range of the groupbox
Set rngGBox = .Range(gbox.TopLeftCell, gbox.BottomRightCell)
'~~> Loop through all shapes
For Each Shp In gbox.Parent.Shapes
If Shp.Type = msoFormControl Then
'~~> Check if the shape is within the groupbox range
If Not Intersect(Shp.TopLeftCell, rngGBox) Is Nothing Then
If Not Shp Is gbox Then
'~~> Check if it is a checkbox
If Shp.FormControlType = xlCheckBox Then
'~~> Check if it is checked
If Shp.ControlFormat.Value = xlOn Then
C_cbox = C_cbox + 1
End If
End If
End If
End If
End If
Next Shp
Next
End With
End Sub
答案 1 :(得分:0)
我不认为您需要一系列复选框。请查看下面的代码。
Sub ResetCheckBoxes()
Dim Ctrl As OLEObject
Dim n As Integer
For Each Ctrl In ActiveSheet.OLEObjects
If TypeName(Ctrl.Object) = "CheckBox" Then
Debug.Print Ctrl.Object.GroupName, Ctrl.Object.Value
Ctrl.Object.Value = True
End If
Next Ctrl
End Sub
代码循环遍历ActiveSheet上的所有ActiveX控件,并选择CheckBoxes。然后,它会在更改值之前打印框的GroupName和Value属性。再次运行代码以查看更改的值。
默认情况下,GroupName是选项卡名称。创建复选框时,您可以手动分配另一个值,也可以使用上面的代码。一旦特定组中的所有复选框具有相同的GroupName,则可以在上面的循环中添加另一个If条件,并仅选择属于该特定组的那些条件,并且这些条件可以满足您的数组目的。