Arrays VBA:在GroupBox数组中选中复选框
我正在运行一个sub,其中我需要计算一个groupbox中选中复选框的数量,并对几个groupbox执行此操作。 编辑:我忘了提到我使用的是表单控件,而不是ActiveX控件 我的第一个问题是创建组框数组。我试着用Arrays VBA:在GroupBox数组中选中复选框,arrays,excel,vba,Arrays,Excel,Vba,我正在运行一个sub,其中我需要计算一个groupbox中选中复选框的数量,并对几个groupbox执行此操作。 编辑:我忘了提到我使用的是表单控件,而不是ActiveX控件 我的第一个问题是创建组框数组。我试着用 GB_Array = Activesheet.Shapes.Range(Array(Cells(x, y), Cells(z, y))) ' x,y,z defined elsewhere 我可以通过手动添加来实现这一点,但这并不理想。我的第二个问题是关于这一部分: Option
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。
编辑:似乎我犯了将分组框与复选框分组的错误,答案适用于“ugnroupped”分组框(因此我可以在不使用复选框的情况下移动分组框)。我认为您不需要复选框数组。请看下面的代码
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控件中循环并选中复选框。然后,在更改值之前,它将打印框的GroupName和Value属性。再次运行代码以查看更改的值
默认情况下,GroupName是选项卡名称。您可以在创建复选框时手动或使用上述代码为其指定另一个值。一旦某个特定组中的所有复选框都具有相同的组名,您就可以向上述循环中添加一个进一步的If条件,并仅选择属于该特定组的复选框,这满足了您创建数组的目的。这就是您要尝试的吗 我的假设:所有控件都是表单控件 我已经对代码进行了注释,所以您在理解它时应该不会有问题。不过,如果您有任何疑问,只需问:) 如果你想使用特定的分组框,那么你可以使用它
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
我的借口:-),但我不能不引用最近关于的评论。不,没关系。我创建它是为了便于用户理解正在发生的事情。在一个真实的场景中,我将结合
Ifs
:)谢谢,这对我来说很有用。编辑:不确定这是否应该是一个单独的问题,但您是否有一种简洁的方法来选择一系列名称作为grpBxNames,这样我就不必手动添加它们了?
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