首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >vba在电子表格中选择名称为x的/unselect所有复选框?

vba在电子表格中选择名称为x的/unselect所有复选框?
EN

Stack Overflow用户
提问于 2016-12-17 22:16:04
回答 1查看 596关注 0票数 0

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

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

代码语言:javascript
复制
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

这将根据需要创建名称为“print”的复选框。

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

代码语言:javascript
复制
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

出于某种原因,只选中了我的一个复选框。我不知道我做错了什么,有人能给我看看吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-17 22:57:02

for each循环更改中:

代码语言:javascript
复制
With Selection
    .Name = "print"

至:

代码语言:javascript
复制
With Selection
    .Name = "print" & i

然后将Sub set_print()更改如下:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41203605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档