Link a Checkbox to a cell in VBA

drannok -  
Pyvoudelet Posted messages 169 Registration date   Status Membre Last intervention   -
Hello everyone,

my question may seem obvious to most of you but... being a novice in VBA, I'm diving in!

Here’s my problem:
I have created a dialog box via a userform in VBA.
In this userform, I have posed two questions and inserted two checkboxes (If they are checked, it means the answer to the corresponding question is "yes")
I would like to link each of these checkboxes to a cell (let's say A1 for Checkbox 1 and A2 for Checkbox 2). This way, the user can return to the dialog box later but their previous answers will remain checked...

Does anyone have a solution to my problem?? I found the linkedcell function but I'm not sure how to use it... please!

3 réponses

gbinforme Posted messages 14930 Registration date   Status Contributeur Last intervention   4 742
 
Hello

To make this work, you need to put a bit of code in your userform, like this for example:

 Private Sub UserForm_Initialize() If Range("A1").Value = "yes" Then CheckBox1.Value = True Else CheckBox1.Value = False End If If Range("A2").Value = "yes" Then CheckBox2.Value = True Else CheckBox2.Value = False End If End Sub Private Sub CheckBox1_Click() If CheckBox1.Value Then Range("A1").Value = "yes" Else Range("A1").Value = "no" End If End Sub Private Sub CheckBox2_Click() If CheckBox2.Value Then Range("A2").Value = "yes" Else Range("A2").Value = "no" End If End Sub 

When you launch your form, the checkboxes take the values from the cells and when you check or uncheck, your cells are updated.
--

Always zen
3
drannok
 
Thank you very much!
I hadn't thought of this solution at all!
Unfortunately, I can only try it on Monday at work ... (well, I shouldn't complain, it's the weekend!)

However, I wonder:
When you write:
Private Sub UserForm_Initialize()
Where do I type that? In the userform's code? Is that possible? (If I click on the checkbox, I type in the checkbox's code, but I can't remember if I can do the same for the userform?)
0
gbinforme Posted messages 14930 Registration date   Status Contributeur Last intervention   4 742
 
Re

All of this is indeed in the code of the userform, which includes the code associated with each component object of the userform. You will also have your code for the buttons (validate or quit for example).

You can therefore copy and paste the total to see how it works, if you have the same names of course.
0
drannok
 
Thank you!
0
Pyvoudelet Posted messages 169 Registration date   Status Membre Last intervention   12
 
Right-click on your checkbox, select "Format Control," go to the "Control" tab, and enter the cell you want to link to this checkbox.

Thus, in this cell, the value "TRUE" or "FALSE" will appear depending on whether the box is checked or not!

You can use this value in any formula. If you don't want the value to appear, write it in white on a white-filled background!!

And there you go! A little simpler than a macro. But I don't know if this works for you!
0
gbinforme Posted messages 14930 Registration date   Status Contributeur Last intervention   4 742
 
Hello,

You forgot this: I created a dialog box via a userform in VBA. and your function does not apply to checkboxes in a userform.
0
Pyvoudelet Posted messages 169 Registration date   Status Membre Last intervention   12
 
Indeed! Sorry for the intrusion.
0