Verificar si una casilla de verificación basada en una hoja de trabajo está marcada

I'm trying to use an IF-clause to determine whether my checkbox, named "Check Box 1", is checked.

Mi código actual:

Sub Button167_Click()
 If ActiveSheet.Shapes("Check Box 1") = True Then
 Range("Y12").Value = 1
 Else
 Range("Y12").Value = 0
 End If
End Sub

This doesn't work. The debugger is telling me there is a problem with the

ActiveSheet.Shapes("Check Box 1")

However, I know this code works (even though it serves a different purpose):

ActiveSheet.Shapes("Check Box 1").Select
With Selection
.Value = xlOn

My checkboxes (there are 200 on my page), are located in sheet1, by the name of "Demande". Each Checkbox is has the same formatted name of "Check Box ...".

preguntado el 31 de julio de 12 a las 14:07

ActiveSheet.Shapes("Check Box 1").Value = True? -

form control checkbox or active x control checkbox? -

@Motes Any way to quickly know, without me having to boot up Parallels? -

Hi, can you please help me to get value of checkbox in javascript in same manner -

You can find another solution using CodeName aquí -

5 Respuestas

Sub Button167_Click()
 If ThisWorkbook.Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value = 1 Then
 Range("Y12").Value = 1
 Else
 Range("Y12").Value = 0
 End If
End Sub

1 is checked, -4146 is unchecked, 2 is mixed (grey box)

Respondido 31 Jul 12, 15:07

This worked like a charm. @Siddharth, your answer is correct as well - Pablo Bernasconi

how to get the value you in javascript code after parsing the excel file. I need the chekbox value from excel workbook stackoverflow.com/questions/34064853/… - anuja patil

¿Es esto lo que estás intentando?

Sub Sample()
    Dim cb As Shape

    Set cb = ActiveSheet.Shapes("Check Box 1")

    If cb.OLEFormat.Object.Value = 1 Then
        MsgBox "Checkbox is Checked"
    Else
        MsgBox "Checkbox is not Checked"
    End If
End Sub

Reemplaza Activesheet with the relevant sheetname. Also replace Check Box 1 with the relevant checkbox name.

Respondido 31 Jul 12, 15:07

This will check the box, but to read it you need to set check the value against 1. - Motas

+1 I didn't know this before. I'm going to delete my reply as it may lead to confusion. Just tried it and indd it works. In this case checked seems to be verified by values 1 and 0 (not true and false). - html_programador

@Motes: Yes. I have added that part. I was reading the post from blackberry so misread it. I thought the user wanted to check the checkbox. :P - Huida de Siddharth

If you reused the shape reference, it might be more efficient, and he probably made it this way for readability. - Motas

@PaoloBernasconi: Could you please explain what do you mean by less efficient and how is it less efficient? - Huida de Siddharth

Parece que en el código de macro VBA para un control de casilla de verificación ActiveX que usa

Si (ActiveSheet.OLEObjects("CheckBox1").Object.Value = True)

y para un control de casilla de formulario que usa

Si (HojaActiva.Formas("CheckBox1").OLEFormat.Object.Value = 1)

Respondido el 18 de Septiembre de 19 a las 03:09

Sobre la base de la anterior respuestas, you can leverage the fact that True es -1 y False is 0 and shorten your code like this:

Sub Button167_Click()
  Range("Y12").Value = _
    Abs(Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0)
End Sub

Si la casilla de verificación está marcada, .Value = 1.

Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0 devoluciones True.

Aplicando el Abs función convierte True a 1.

If the checkbox is unchecked, .Value = -4146.

Worksheets(1).Shapes("Check Box 1").OLEFormat.Object.Value > 0 devoluciones False.

Aplicando el Abs función convierte False a 0.

contestado el 23 de mayo de 17 a las 13:05

Hi, can you please help me to get value of checkbox in javascript in same manner - anuja patil

Tratar: Controls("Check Box 1") = True

Respondido 31 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.