简体   繁体   English

VBA选择/取消选择电子表格中名称为x的所有复选框?

[英]vba select /unselect all checkboxes with name x in spreadsheet?

I am trying to select/deselect all checkboxes with the name 'print' on a spreadsheet if another checkbox 'print1' is checked. 如果选中了另一个复选框“ print1”,我试图选择/取消选择电子表格上名称为“ print”的所有复选框。

I am creating my checkboxes using the following code as part as a for each loop. 我使用以下代码作为每个循环的一部分来创建我的复选框。

For Each objFile In objFolder.Files


If DatePart("ww", objFile.DateCreated, vbMonday, vbFirstFourDays) = Range("H7").Value Then


'print file PG
    Cells(i + 13, 1) = Range("T7").Value
    'print file Month
    Cells(i + 13, 5) = Range("H7").Value

    'print file Year
    Cells(i + 13, 9) = Range("B7").Value

    'print file name
    Cells(i + 13, 13) = objFile.Name

    'print file path
    Cells(i + 13, 18) = "=hyperlink(""" & objFile.Path & """)"

     'add action box 1
    ActiveSheet.CheckBoxes.Add(Cells(i + 13, 27).Left, Cells(i + 13, 27).Top, Cells(i + 13, 27).Width, Cells(i + 13, 27).Height).Select

    With Selection
    .Name = "print"
        .Caption = ""
        .Value = xlOff '
        .LinkedCell = Cells(i + 13, 27)
        .Display3DShading = False
            End With

This creates as many checkboxes with the name 'print' as required. 这将根据需要创建多个名为“ print”的复选框。

I also have a unique checkbox named 'print1'. 我还有一个唯一的复选框,名为“ print1”。 This checkbox has a macro assigned to it called set_print. 此复选框具有分配给它的名为set_print的宏。 The macro should trigger when the user checks / unchecks this checkbox and should check/uncheck all of my other checkboxes named 'print'. 当用户选中/取消选中此复选框时,宏应该触发,并且应该选中/取消选中我所有其他名为“ print”的复选框。 TO do this I am using the following code: 为此,我使用以下代码:

Sub set_print()
If ActiveSheet.CheckBoxes("print").Value <> xlOn Then
ActiveSheet.CheckBoxes.Value = xlOn
ActiveSheet.Shapes("Search1").TextFrame.Characters.Text = "Print"
Else
ActiveSheet.CheckBoxes("print").Value = xlOff
ActiveSheet.Shapes("Search1").TextFrame.Characters.Text = "Search"
End If
End Sub

For some reason, only one of my checkboxes is checked. 由于某种原因,仅选中了我的一个复选框。 I am not sure what I am doing wrong, please can someone show me? 我不确定自己做错了什么,请有人告诉我吗?

in the for each loop change: for each循环更改中:

With Selection
    .Name = "print"

to: 至:

With Selection
    .Name = "print" & i

then change Sub set_print() as follows: 然后更改Sub set_print() ,如下所示:

Sub set_print()
    With ActiveSheet
        If .CheckBoxes("print1").Value <> xlOn Then
            CheckThem xlOn
            .Shapes("Search1").TextFrame.Characters.Text = "Print"
        Else
            CheckThem xlOff
            .Shapes("Search1").TextFrame.Characters.Text = "Search"
        End If
    End With
End Sub

Sub CheckThem(xlOnOff As Long)
    Dim chkBx As CheckBox

    With ActiveSheet
        For Each chkBx In .CheckBoxes
            If Left(chkBx.Name, 5) = "print" Then chkBx.Value = xlOnOff
        Next
    End With
End Sub

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

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