【问题标题】:Get Values of all Check Boxes in Excel using VBA使用VBA获取Excel中所有复选框的值
【发布时间】:2010-11-27 10:35:02
【问题描述】:

我使用以下代码以编程方式将许多复选框添加到 Excel 工作表:

With ActiveSheet.CheckBoxes.Add(rCell.Left, rCell.Top, rCell.Width, rCell.Height)
        .Interior.ColorIndex = xlNone
        .Caption = ""
End With

现在我需要一个代码来解析工作表中存在的所有复选框并获取它们的值(真或假)以及它们所在的单元格。 如何做到这一点?

谢谢...

【问题讨论】:

    标签: excel checkbox vba


    【解决方案1】:
    Sub Add_CheckBoxes()
    
    With ActiveSheet.CheckBoxes.Add(ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height)
        .Interior.ColorIndex = xlNone
        .Caption = ""
    End With
    
    For Each chk In ActiveSheet.CheckBoxes
        If chk Then MsgBox "Checked"
    Next
    End Sub
    

    【讨论】:

    • 感谢您的回复!有用!但是有什么方法可以让我得到复选框所在的单元格?
    • 不知道能不能直接找到。复选框同时具有代表其位置的 Left 属性和 Top 属性。您可以依次将 chk.Left 与每列的 Left 属性进行比较 - ActiveSheet.Columns(1).Left 等 - 直到找到最接近的匹配项,然后依次重复 Top 属性和每一行
    【解决方案2】:

    添加复选框后,您可以像这样遍历它们:

    Sub Checkbox_Test()
    
    Dim chk As CheckBox
    Dim cell As Range
    
    For Each chk In ActiveSheet.CheckBoxes
        If chk.Value = Checked Then        '1 is true, but the constant
            Set cell = chk.TopLeftCell     ' "Checked" avoids "magic numbers".
    
            'do whatever with cell
            MsgBox cell.Address
        End If
    Next chk
    

    结束子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 2017-10-15
      • 2016-04-14
      • 2016-01-11
      • 2017-04-03
      • 2014-09-11
      相关资源
      最近更新 更多