user14061418
user14061418

Reputation:

VBA Userforms - How to keep checkbox selections updated?

So the title may not make a lot of sense, so I'll try to explain as best as I can. As you can see in the image below, I have multiple checkboxes on the left (let's just focus on those now).

Everytime I tick certain checkboxes and click on "Next step" the captions of those checkboxes are added to a combobox.

Let's imagine that I select the first 2 and click on "Next Step". While I'm on the next step I realize I don't want one of the selected checkboxes and I want to remove it. If I go back and deselect it, it doesn't remove if from the combobox and it's not like I can just delete it from there.

enter image description here

The code I'm using to send the selected checkbox captions to the combobox is as follows:

If speGro = True And speGroT = True Then
    SEG_MODULE.seg_cbb_selDim.AddItem "Caption Name"
End If

Basically I repeat this if statement for all checkboxes.

Is there a way to make sure ONLY the selected checkboxes appear on the combobox?

Thank you for the help!

Upvotes: 1

Views: 129

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149277

When you uncheck it, would you be pressing "Next Step" or do you want that to happen automatically? – Siddharth Rout 41 mins ago

I would be pressing "Next Step" again. :) – soraia635 23 mins ago

Since you are pressing Next Step, then it is really simple actually ;) Clear the combobox. The checked items will automatically get added again. No need to remove specific items. Lets say your userform has Checkbox1-Checkbox2, ComboBox1-ComboBox2 and CommandButton1 then you can do something like this

Option Explicit

Private Sub CommandButton1_Click()
    ComboBox1.Clear
    ComboBox2.Clear
    
    If CheckBox1.Value = True Then ComboBox1.AddItem CheckBox1.Caption
    If CheckBox2.Value = True Then ComboBox1.AddItem CheckBox2.Caption
    
    If CheckBox1.Value = True Then ComboBox2.AddItem CheckBox1.Caption
    If CheckBox2.Value = True Then ComboBox2.AddItem CheckBox2.Caption
End Sub

Upvotes: 0

Related Questions