使用VBA在Excel中选择CheckBoxes

时间:2015-09-05 20:17:52

标签: vba excel-vba excel

我有一张excel表格,其中包含范围“D12”到“D26”的复选框,点击邻居我想选中所有复选框,但仅基于范围,即Range("D12:D26")中的所有复选框。

我正在使用的代码如下,但不适合我:

Private Sub SelectALL_Click()

    Dim cells As Range
    Dim rng As Range

    Set rng = Sheet1.Range("D12:D14")



    For Each cells In rng
        cells.Select
    Next


End Sub

1 个答案:

答案 0 :(得分:3)

由于您尚未提及它是什么类型的控件(ActiveX或Form Control),请选择

表单控件示例

Sub FormControl_Example()
    Dim shp As Shape
    Dim rng As Range, rngShp As Range

    With ThisWorkbook.Sheets("Sheet1") '<~~ Change this to the relevant sheet name
        Set rng = .Range("D12:D14")

        For Each shp In .Shapes
            Set rngShp = .Range(shp.TopLeftCell.Address)

            If Not Intersect(rngShp, rng) Is Nothing Then
                shp.OLEFormat.Object.Value = True
            End If
        Next
    End With
End Sub

ActiveX控件示例

Sub ActiveX_Example()
    Dim shp As Shape
    Dim rng As Range, rngShp As Range

    With ThisWorkbook.Sheets("Sheet1") '<~~ Change this to the relevant sheetname
        Set rng = .Range("D12:D14")

        For Each shp In .Shapes
            Set rngShp = .Range(shp.TopLeftCell.Address)

            If Not Intersect(rngShp, rng) Is Nothing Then
                .OLEObjects(shp.Name).Object.Value = True
            End If
        Next
    End With
End Sub