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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1409744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:59:46  来源:igfitidea点击:

Get Values of all Check Boxes in Excel using VBA

excel-vbacheckboxvbaexcel

提问by Manoj

I add many check boxes to the excel sheet programaticaly using the following code:

我使用以下代码以编程方式向 Excel 表中添加了许多复选框:

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

Now I need a code which would parse through all the Check boxes that are present in the sheet and get their value(true or false) and also the cell at which they are present. How to do this?

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

Thanks...

谢谢...

回答by systemovich

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

回答by Fink

Once you add the checkboxes you can loop through them like this:

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

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

结束子