excelvbacheckboxactivexmultiple-conditions

Using ActiveX Checkboxes Conditions to Check or Disable Another Checkbox AND to Populate Column


Fair warning I'm very new to using VBA and I've looked all over this site but I can't seem to a similar question.
I'm hoping it's a pretty easy solution though.

I have these checkboxes 1 - 4

Example of data

Column A (CheckBox 1) Column B (CheckBox2) Column C (CheckBox3) Column D (CheckBox4) Column E Results
1 4 7 E1
2 5 8 E2
3 6 9 E3

Here's a summary of the necessary steps/ conditions

Checkboxes 1 - 3 affect each other and all three affect checkbox 4

So for example what should happen is that if I select CheckBox1 in column A, CheckBox2 ,3 and 4 will disable

The data in column A will transfer to column E

And if CheckBox 1 - 3 are not checked CheckBox4 will enable and automatically be checked while E1:E3 will be 0

Right now,

If I run the code the first time around and checkboxes 1 -3 are not clicked then checkbox 4 is clicked. GOOD!
If I click any of checkbox 1 -3, checkbox 4 is not clicked and it is disabled GOOD!
If I deselect checkbox 1 - 3 checkbox 4 is permanently disabled and does not reselect. BAD!
I can select a new checkbox in 1 - 3 and the other two will still disable and column E will change. GOOD!

here's my code so far

Private Sub CheckBox1_Click() 

If CheckBox1.Value = True Then 
    CheckBox2.Value = False 
    CheckBox2.Enabled = False 
    CheckBox3.Value = False 
    CheckBox3.Enabled = False 
    CheckBox4.Value = False 
    CheckBox4.Enabled = False 
    Range("A1:A3").Value = Range("E1:E3").Value

Else: 
    CheckBox2.Enabled = True 
    CheckBox3.Enabled = True

End If 
End Sub 

Private Sub CheckBox2_Click() 

If CheckBox2.Value = True Then 
    CheckBox1.Value = False 
    CheckBox1.Enabled = False 
    CheckBox3.Value = False 
    CheckBox3.Enabled = False
    CheckBox4.Value = False 
    CheckBox4.Enabled = False 
    Range("B1:B3").Value = Range("E1:E3").Value

Else:
    CheckBox1.Enabled = True 
    CheckBox3.Enabled = True

End If 
End Sub 

Private Sub CheckBox3_Click() 

If CheckBox3.Value = True Then 
    CheckBox1.Value = False 
    CheckBox1.Enabled = False 
    CheckBox2.Value = False 
    CheckBox2.Enabled = False 
    CheckBox4.Value = False 
    CheckBox4.Enabled = False 
    Range("A1:A3").Value = Range("E1:E3").Value

Else: 
    CheckBox1.Enabled = True 
    CheckBox2.Enabled = True

End If 
End Sub 

Private Sub CheckBox9_Click() 

If CheckBox1.Value = False And CheckBox2.Value = False And CheckBox3.Value = False Then 
    CheckBox4.Value = True 
    CheckBox4.Enabled = True 
    Range("E1:E3").Value = 0

Else: 
    CheckBox4.Value = False 
    CheckBox4.Enabled = False

End If 
End Sub 
"I tried using a loop code I found online for each sub which didn't work" 
"And then I tried with just looping the end at checkbox 4 
same thing checkbox permanently disabled."

Sub CheckboxLoop() 
 Dim objX As OLEObject 

With ActiveSheet 
 For Each objX In .OLEObjects
End If 
 Next 
 End With 
 End Sub 

I also tried deleting the CheckBox4.Enabled = False at various points in the code. Once I deleted it all except last Sub Private Sub CheckBox4_Click() and other times deleted just last sub but it would also just be stuck on disabled. Eventually I deleted all CheckBox4.Enabled = False but of course that means check box 4 won't disable.

Any help would be greatly appreciated.


Solution

  • This mod doesn't iterate through all checkboxes, only add some lines to the original code.
    Checks every click the state of all checkboxes, and if all False then enable them and activate the Checkbox4.
    Also there were assignment mismatch of ranges (probably caused by copying).

    Private Sub CheckBox1_Click()
    
    If CheckBox1.value = False And CheckBox2.value = False And CheckBox3.value = False Then
        CheckBox4.value = True
        CheckBox4.Enabled = True
        CheckBox2.Enabled = True
        CheckBox3.Enabled = True
        Range("E1:E3").value = 0
    ElseIf CheckBox1.value = True Then
        CheckBox2.value = False
        CheckBox2.Enabled = False
        CheckBox3.value = False
        CheckBox3.Enabled = False
        CheckBox4.value = False
        CheckBox4.Enabled = False
        Range("E1:E3").value = Range("A1:A3").value
    
    Else
        CheckBox2.Enabled = True
        CheckBox3.Enabled = True
    
    End If
    End Sub
    
    Private Sub CheckBox2_Click()
    
    If CheckBox1.value = False And CheckBox2.value = False And CheckBox3.value = False Then
        CheckBox4.value = True
        CheckBox4.Enabled = True
        CheckBox1.Enabled = True
        CheckBox3.Enabled = True
        Range("E1:E3").value = 0
    ElseIf CheckBox2.value = True Then
        CheckBox1.value = False
        CheckBox1.Enabled = False
        CheckBox3.value = False
        CheckBox3.Enabled = False
        CheckBox4.value = False
        CheckBox4.Enabled = False
        Range("E1:E3").value = Range("B1:B3").value
    
    Else
        CheckBox1.Enabled = True
        CheckBox3.Enabled = True
    
    End If
    End Sub
    
    Private Sub CheckBox3_Click()
    
    If CheckBox1.value = False And CheckBox2.value = False And CheckBox3.value = False Then
        CheckBox4.value = True
        CheckBox4.Enabled = True
        CheckBox1.Enabled = True
        CheckBox2.Enabled = True
        Range("E1:E3").value = 0
    ElseIf CheckBox3.value = True Then
        CheckBox1.value = False
        CheckBox1.Enabled = False
        CheckBox2.value = False
        CheckBox2.Enabled = False
        CheckBox4.value = False
        CheckBox4.Enabled = False
        Range("E1:E3").value = Range("C1:C3").value
    
    Else:
        CheckBox1.Enabled = True
        CheckBox2.Enabled = True
    
    End If
    End Sub
    
    Private Sub CheckBox9_Click()
    
    If CheckBox1.value = False And CheckBox2.value = False And CheckBox3.value = False Then
        CheckBox4.value = True
        CheckBox4.Enabled = True
        Range("E1:E3").value = 0
    
    Else:
        CheckBox4.value = False
        CheckBox4.Enabled = False
    
    End If
    End Sub
    
    

    SIDENOTE: The CheckBox9_Click() Sub is not needed afterward.