简体   繁体   English

选择/取消选择范围EXCEL VBA宏中的所有复选框

[英]Select / unselect all CheckBoxes in a range EXCEL VBA macro

I am creating a VBA macro to select all checkboxes in a sheet and it works fine but now i want to adjust my code to select and unselect just the checkboxes in a particular range. 我正在创建一个VBA宏以选择工作表中的所有复选框,并且工作正常,但现在我想调整代码以选择和取消选择特定范围内的复选框。

Here is my code. 这是我的代码。

Sub Select_all()

Dim Cbox As CheckBox
Dim Rng As Range

Set Rng = ActiveWorkbook.Sheets("Sheet4").Range("B7, B104")

For Each Cbox In ActiveSheet.CheckBoxes

If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing Then
  If Cbox.name <> ActiveSheet.CheckBoxes("Check Box 104").name Then
    Cbox.Value = ActiveSheet.CheckBoxes("Check Box 104").Value
  End If
End If

Next Cbox

End Sub

I added 我加了

If Not Intersect(Cbox.TopLeftCell, Rng) Is Nothing 如果不相交(Cbox.TopLeftCell,Rng)什么都没有

but it doesn't change anything, and when i remove this part then it selects all the checkboxes in the sheet and i need only for the Range("B7:B104") 但它并没有改变任何东西,当我删除此部分时,它选择了工作表中的所有复选框,而我只需要Range(“ B7:B104”)

Any suggestions please ? 有什么建议吗? Thank you very much. 非常感谢你。

This works for me. 这对我有用。 But I changed the cb# to "1" because I don't have 104 of them on my test sheet. 但是我将cb#更改为“ 1”,因为我的测试表中没有104。 Any cb's in range B7:B104 will be changed to the value of cb1, which you can change to cb104 in your script. 范围B7:B104中的所有cb都将更改为cb1的值,您可以在脚本中将其更改为cb104。

Sub Link_Checkboxes_To_Cells()

    Dim cBox As CheckBox

        For Each cBox In Sheet1.CheckBoxes
            If Not Intersect(cBox.TopLeftCell, Range("B7:B104")) Is Nothing Then
                If cBox.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
                    cBox.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
                End If
            End If
        Next cBox

    End Sub

@JuniorDev @JuniorDev

There are a few reasons why the above code doesn't work for you. 上面的代码对您不起作用的原因有很多。 I'll try to list them. 我会尝试列出它们。

The above code works for "Form Control" checkboxes - Not ActiveX type checkboxes. 上面的代码适用于“表单控件”复选框-非ActiveX类型复选框。 You would need a different code for ActiveX checkboxes. 您需要为ActiveX复选框使用其他代码。 Or you may not have a form checkbox named "Check Box 1". 或者您可能没有名为“复选框1”的表单复选框。 But I think that would give you an error. 但是我认为那会给你一个错误。 Or your other checkboxes may not be in the Range("B7:B104"). 否则您的其他复选框可能不在Range(“ B7:B104”)中。 You can try changing that to Range("A1:Z10000") to insure your checkboxes are in the range. 您可以尝试将其更改为Range(“ A1:Z10000”)以确保您的复选框在范围内。

If you are not sure what type checkboxes you have then you can run the following code to find their names and what type of control they are, either form or activex. 如果不确定所用的复选框类型,则可以运行以下代码以查找其名称以及它们的控件类型(表单还是Activex)。 It will print the info in your immediate window of the VB editor. 它将在您的VB编辑器的直接窗口中打印信息。

Sub CheckboxLoop()
'PARTIAL SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim cb As Shape

'Loop through Form Checkboxes
  For Each cb In ActiveSheet.Shapes
    If cb.Type = msoFormControl Then
      If cb.FormControlType = xlCheckBox Then
            Debug.Print "Form Control: " & cb.Name
      End If
    End If
  Next cb

'Loop through ActiveX Checkboxes
Dim ChkBx As OLEObject
For Each ChkBx In ActiveSheet.OLEObjects
  If TypeName(ChkBx.Object) = "CheckBox" Then
        Debug.Print "ActiveX Control: " & ChkBx.Name
  End If
Next ChkBx
End Sub

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

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