使用VBA获取Excel中所有复选框的值

时间:2009-09-11 08:39:24

标签: excel-vba checkbox vba excel

我使用以下代码在programaticaly中添加了许多复选框:

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

现在我需要一个代码来解析工作表中存在的所有复选框并获取它们的值(true或false)以及它们所在的单元格。 怎么做?

...谢谢

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分: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

End Sub