Combox list depend on OptionButton value

flameash

New Member
Joined
Feb 9, 2017
Messages
3
Hi all,
I'm trying to include 2 lists for my Combox listing, but it's not working....


Here's my code and I hope someone here is able to help out
Private Sub OptionButton1_Click()


End Sub


Private Sub OptionButton2_Click()


End Sub


Private Sub OptionButton3_Click()


End Sub


Private Sub OptionButton5_Click()


End Sub


Private Sub OptionButton6_Click()


End Sub



Private Sub ComboBox1_Change()


End Sub


Private Sub ComboBox2_Change()


End Sub
Private Sub ComboBox3_Change()


End Sub
Private Sub UserForm_Initialize()

OptionButton1.Value = True
OptionButton2.Value = False
OptionButton3.Value = True
OptionButton4.Value = False
OptionButton5.Value = True
OptionButton6.Value = False

ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear




If OptionButton1 = True Then
With UserForm3.ComboBox1
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
.AddItem "5"
.AddItem "6"
.AddItem "7"
End With
Else If OptionButton2 = True Then
With UserForm3.ComboBox1
.AddItem "2"
.AddItem "4"
.AddItem "6"
.AddItem "8"
End With
End If


If OptionButton3 = True Then
With UserForm3.ComboBox2
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
.AddItem "5"
.AddItem "6"
.AddItem "7"
End With
Else If OptionButton4 = True Then
With UserForm3.ComboBox2
.AddItem "2"
.AddItem "4"
.AddItem "6"
.AddItem "8"
End With
End If



If OptionButton5 = True Then
With UserForm3.ComboBox3
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
.AddItem "5"
.AddItem "6"
.AddItem "7"
End With
Else If OptionButton6 = True Then
With UserForm3.ComboBox3
.AddItem "2"
.AddItem "4"
.AddItem "6"
.AddItem "8"
End With
End If


So the codes runs without issue, but if I select OptionButton 2 or 4 or 6, ComboBox still has the option of 1-7, not 2,4,6,8

I would be grateful if someone can share insignts
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi!

The problem you are having is that this code:
Code:
If OptionButton1 = True Then
     With UserForm3.ComboBox1
    .AddItem "1"
    .AddItem "2"
    .AddItem "3"
    .AddItem "4"
    .AddItem  "5"
    .AddItem "6"
    .AddItem "7"
    End With
 Else If OptionButton2 = True Then
     With UserForm3.ComboBox1
    .AddItem "2"
    .AddItem "4"
    .AddItem "6"
    .AddItem "8"
    End With
 End If


 If OptionButton3 = True Then
     With UserForm3.ComboBox2
    .AddItem "1"
    .AddItem "2"
    .AddItem "3"
    .AddItem "4"
    .AddItem  "5"
    .AddItem "6"
    .AddItem "7"
    End With
 Else If OptionButton4 = True Then
     With UserForm3.ComboBox2
    .AddItem "2"
    .AddItem "4"
    .AddItem "6"
    .AddItem "8"
    End With
 End If



 If OptionButton5 = True Then
     With UserForm3.ComboBox3
    .AddItem "1"
    .AddItem "2"
    .AddItem "3"
    .AddItem "4"
    .AddItem  "5"
    .AddItem "6"
    .AddItem "7"
    End With
 Else If OptionButton6 = True Then
     With UserForm3.ComboBox3
    .AddItem "2"
    .AddItem "4"
    .AddItem "6"
    .AddItem "8"
    End With
 End If

is in your Userform_Initialize event. Thus, it only is read in and occurs at the time the user form is loaded or shown. What you could do is simply put each combobox add item routine in the corresponding Option button Click event. For example:

Code:
Private Sub OptionButton1_Click()
With UserForm3.ComboBox1
   .AddItem "1"
   .AddItem "2"
   .AddItem "3"
   .AddItem "4"
   .AddItem  "5"
   .AddItem "6"
   .AddItem "7"
End With
End Sub


 Private Sub OptionButton2_Click()
 With UserForm3.ComboBox1
     .AddItem "2"
     .AddItem "4"
     .AddItem "6"
     .AddItem "8"
End With

 End Sub

Regards,

CJ
 
Last edited:
Upvote 0
I also should have put in

Code:
ComboBox1.Clear

before each With statement in each Optionbutton_Click sub.

Regards again,

CJ
 
Upvote 0
Just in case you would like to see another way. You can load items in a combobox like this.
A loop will add in the numbers.
The 1st script loads in 1 to 7
The second script loads in 2 to 8 skipping numbers so you would get 2,4,6,8

Code:
Private Sub CommandButton1_Click()
Dim i As Long
ComboBox1.Clear
   For i = 1 To 7
   ComboBox1.AddItem i
   Next
End Sub

Private Sub CommandButton2_Click()
Dim i As Long
ComboBox2.Clear
   For i = 2 To 8 Step 2
   ComboBox2.AddItem i
   Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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