checkbox code

DB73

Board Regular
Joined
Jun 7, 2022
Messages
102
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2010
  6. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Peeps,

Was busy with my userform and checkbox.
so with some help from this forum got myself a code.
but i have a question
as u can see after "else" i got the items that has to be uncheckt.
but it already works if i "activate" "uncheck ";
VBA Code:
        ComboBox1.Enabled = True
        ComboBox1.Value = ""
        ComboBox1.BackColor = RGB(255, 255, 255)

why is it not necessary to put the rest in ??
also i tried to use this;
VBA Code:
        'ComboBox1.Enabled = True
        'ComboBox1.Value = ""
        'ComboBox1.BackColor = RGB(255, 255, 255)

but then it doesnt work en the comboboxes stay gry as checked the checkbox

this is the complete code:

VBA Code:
Private Sub CheckBox1_Click()

   If CheckBox1.Value = True Then
  
        ComboBox1.Enabled = False
        ComboBox1.Value = "enkel rit registratie"
        ComboBox1.BackColor = RGB(210, 210, 210)
  
        ComboBox4.Enabled = False
        ComboBox4.Value = "0"
        ComboBox4.BackColor = RGB(210, 210, 210)
       
        ComboBox5.Enabled = False
        ComboBox5.Value = "0"
        ComboBox5.BackColor = RGB(210, 210, 210)
       
        ComboBox6.Enabled = False
        ComboBox6.Value = "0"
        ComboBox6.BackColor = RGB(210, 210, 210)
       
        ComboBox7.Enabled = False
        ComboBox7.Value = "0"
        ComboBox7.BackColor = RGB(210, 210, 210)
       
        ComboBox8.Enabled = False
        ComboBox8.Value = "0"
        ComboBox8.BackColor = RGB(210, 210, 210)
       
        ComboBox9.Enabled = False
        ComboBox9.Value = "0"
        ComboBox9.BackColor = RGB(210, 210, 210)
       
        ComboBox14.Enabled = False
        ComboBox14.Value = "zakelijke rit"
        ComboBox14.BackColor = RGB(210, 210, 210)
       
        ComboBox16.Enabled = True
        ComboBox16.Value = ""
        ComboBox16.BackColor = RGB(255, 255, 255)
       
        TextBox1.Enabled = False
        TextBox1.Value = "0"
        TextBox1.BackColor = RGB(210, 210, 210)
       
    Else
        ComboBox1.Enabled = True
        ComboBox1.Value = ""
        ComboBox1.BackColor = RGB(255, 255, 255)
  
        'ComboBox4.Enabled = True
        'ComboBox4.Value = ""
        'ComboBox4.BackColor = RGB(255, 255, 255)
       
        'ComboBox5.Enabled = True
        'ComboBox5.Value = ""
        'ComboBox5.BackColor = RGB(255, 255, 255)
       
        'ComboBox6.Enabled = True
        'ComboBox6.Value = ""
        'ComboBox6.BackColor = RGB(255, 255, 255)
       
        'ComboBox7.Enabled = True
        'ComboBox7.Value = ""
        'ComboBox7.BackColor = RGB(255, 255, 255)
       
        'ComboBox8.Enabled = True
        'ComboBox8.Value = ""
        'ComboBox8.BackColor = RGB(255, 255, 255)
       
        'ComboBox9.Enabled = True
        'ComboBox9.Value = ""
        'ComboBox9.BackColor = RGB(255, 255, 255)
       
        'ComboBox14.Enabled = True
        'ComboBox14.Value = ""
        'ComboBox14.BackColor = RGB(255, 255, 255)
       
        'TextBox1.Enabled = True
        'TextBox1.Value = ""
        'TextBox1.BackColor = RGB(255, 255, 255)
       
    End If

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
also i tried to use this;
VBA Code:
'ComboBox1.Enabled = True
'ComboBox1.Value = ""
'ComboBox1.BackColor = RGB(255, 255, 255)
but then it doesnt work en the comboboxes stay gry as checked the checkbox
All that green code is commented out so it will do nothing. If you realize that already, then I'm not following. Otherwise, remove the leading apostrophe's and see if that solves your issue. If your combos were consecutively numbered (e.g. 1 thru 9) you could shorten the code considerably.
 
Upvote 0
All that green code is commented out so it will do nothing. If you realize that already, then I'm not following. Otherwise, remove the leading apostrophe's and see if that solves your issue. If your combos were consecutively numbered (e.g. 1 thru 9) you could shorten the code considerably.
thats what i already knew.....at first i put it in without the apostrophe, but then i tried what could be removed.
almost everything except the one withe apostrophe...thats what i questioned about....why is it working....the green is not necessary....but i cant remove from combobox1...if i do that i doesnt work anymore
 
Upvote 0
This seems to work in that it switches the appearance of the combo:
VBA Code:
If CheckBox1.Value = True Then
     ComboBox1.Enabled = False
     ComboBox1.Value = "enkel rit registratie"
     ComboBox1.BackColor = RGB(210, 210, 210)
Else
     ComboBox1.Enabled = True
     ComboBox1.Value = ""
     ComboBox1.BackColor = RGB(255, 255, 255)
End If
If I comment out the Else part, it changes once and stays that way no matter how many times I click the checkbox, which makes sense to me. I guess I can't help then, because I don't understand what the problem is, or even if there is a problem.
 
Upvote 0
This seems to work in that it switches the appearance of the combo:
VBA Code:
If CheckBox1.Value = True Then
     ComboBox1.Enabled = False
     ComboBox1.Value = "enkel rit registratie"
     ComboBox1.BackColor = RGB(210, 210, 210)
Else
     ComboBox1.Enabled = True
     ComboBox1.Value = ""
     ComboBox1.BackColor = RGB(255, 255, 255)
End If
If I comment out the Else part, it changes once and stays that way no matter how many times I click the checkbox, which makes sense to me. I guess I can't help then, because I don't understand what the problem is, or even if there is a problem.
and thats what i dont get...thats not happening...only the first part from combobox1 is necessary ...it works without the rest of it...so i can toggle the checkbox on or off...the rest is "useless" to get it working from on and off, and i thought it was necessary to get everything back if the checkbox is "off"


sorry, my English istnt that good, so i hope u understand what i mean
 
Upvote 0
Maybe if you stepped through the code once when checking and again when unchecking and watch the flow it might make sense to you or reveal why it seems to work the way it does. Perhaps code execution branches off to somewhere that you're not aware of, and that has something to do with it. Unless you want to post a copy of the file somewhere I guess there's not much else I can do to answer the question.
 
Upvote 0
Hi,
I have only had quick glance at your code & following not fully tested but may give you something to work with in your project

VBA Code:
Private Sub CheckBox1_Click()
    Dim i           As Variant
    Dim state       As Boolean
    
    state = Me.CheckBox1.Value

    For Each i In Array(1, 4, 5, 6, 7, 8, 9, 14, 16)
    
        With Me.Controls("ComboBox" & i)
        
            .Enabled = Not state
                                        'TRUE               'FALSE
            .BackColor = IIf(.Enabled, RGB(255, 255, 255), RGB(210, 210, 210))
            
            .Value = IIf(Not state And i = 1, "enkel rit registratie", _
                     IIf(Not state And i = 14, "zakelijke rit", ""))
        End With
        
    Next i
    
    With Me.TextBox1
        .Enabled = Not state
                                    'TRUE               'FALSE
        .BackColor = IIf(.Enabled, RGB(255, 255, 255), RGB(210, 210, 210))
        .Value = IIf(Not state, 0, "")
    End With
    
End Sub

Solution may not be fully correct & should be adjusted as required to meet your specific project need.

Dave
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top