Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Values of all Check Boxes in Excel using VBA

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

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?

Thanks...

like image 755
Manoj Avatar asked Sep 11 '09 08:09

Manoj


2 Answers

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
like image 185
Geoffrey Avatar answered Sep 27 '22 22:09

Geoffrey


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

like image 40
Fink Avatar answered Sep 28 '22 00:09

Fink