Obtenga los valores de todas las casillas de verificación en Excel usando VBA
Pregunta
Agrego muchas casillas de verificación a la hoja de Excel mediante programación con el siguiente código:
With ActiveSheet.CheckBoxes.Add(rCell.Left, rCell.Top, rCell.Width, rCell.Height)
.Interior.ColorIndex = xlNone
.Caption = ""
End With
Ahora necesito un código que se pueda analizar a través de todas las casillas de verificación que están presentes en la hoja y obtener su valor (verdadero o falso) y también la celda en la que están presentes. ¿Cómo hacer esto?
Gracias ...
Solución
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
Otros consejos
Una vez que agregue las casillas de verificación, podrá recorrerlas de la siguiente manera:
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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow