简体   繁体   English

使用VBA在Excel中选择CheckBoxes

[英]Select CheckBoxes in Excel using VBA

I have a excel sheet having check boxes from Range "D12" to "D26", on click of abutton I want to select all checkboxes but based on only range ie all checkboxes in Range("D12:D26") . 我有一个excel表,从“D12”到“D26”的复选框,点击邻居我想选择所有复选框,但仅基于范围,即范围内的所有复选框Range("D12:D26")

Code which I am using is below, but not working for me: 我正在使用的代码如下,但不适用于我:

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

Since you have not mentioned what kind of control is it (ActiveX or Form Control), take your pick 既然你没有提到它是什么类型的控件(ActiveX或表单控件),请选择

Form Control Example 表格控制示例

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 Control Example 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM