【问题标题】:vba select /unselect all checkboxes with name x in spreadsheet?vba选择/取消选择电子表格中名称为x的所有复选框?
【发布时间】:2017-05-03 09:13:13
【问题描述】:

如果选中了另一个复选框“print1”,我正在尝试选择/取消选择电子表格上所有名为“print”的复选框。

我正在使用以下代码创建我的复选框,作为每个循环的一部分。

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

这会根据需要创建多个名称为“打印”的复选框。

我还有一个名为“print1”的独特复选框。这个复选框有一个分配给它的宏,称为 set_print。当用户选中/取消选中此复选框并应选中/取消选中我所有其他名为“打印”的复选框时,宏应触发。为此,我使用以下代码:

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

由于某种原因,我只选中了一个复选框。我不确定我做错了什么,请有人告诉我吗?

【问题讨论】:

    标签: excel vba checkbox


    【解决方案1】:

    for each循环改变:

    With Selection
        .Name = "print"
    

    到:

    With Selection
        .Name = "print" & i
    

    然后将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
    

    【讨论】:

    • 这不起作用,这只会检查所有复选框。我只想检查那些名为“打印”的复选框
    猜你喜欢
    • 2017-10-15
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2011-10-09
    • 2014-06-29
    相关资源
    最近更新 更多