Random selection of comboboxes

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
171
Hi there,

I have a userform with two option buttons and four comboboxes.

If Optminor = True then combox 1, 2 and 4 is visible. If Optmajor=true then Combobox 3 & 4 are visible.

Combobox 1 & 2 have constant list values but combobox 4 varies dependent on the value of combobox 1 & 2.
Combobox 3 has a fixed list but again combobox 4 varies dependent on the value of combobox 3.

What I am hoping to do is have a button that will make a random selection. Firstly randomly choose between the two option buttons and then between the variety of comboboxes as mentioned above.

Is this possible at all?

Thanks in advance for any help you can give,

Mike
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
13,588
Office Version
  1. 2007
Platform
  1. Windows
I did not understand the second part:
and then between the variety of comboboxes as mentioned above.

Your data should be in sheet1, column A for combo1, B for 2, C for 3 and column D for 4. And start in row 2

Try this

Code:
Dim sh As Worksheet   'At the beginning of the entire code.


Private Sub ComboBox2_Change()
    For i = 2 To sh.Range("A" & Rows.Count).End(xlUp).Row
        If sh.Cells(i, "A").Value = ComboBox1.Value And sh.Cells(i, "B").Value = ComboBox2.Value Then
            ComboBox4.AddItem sh.Cells(i, "D").Value
        End If
    Next
End Sub


Private Sub ComboBox3_Change()
    For i = 2 To sh.Range("C" & Rows.Count).End(xlUp).Row
        If sh.Cells(i, "C").Value = ComboBox3.Value Then
            ComboBox4.AddItem sh.Cells(i, "D").Value
        End If
    Next
End Sub


Private Sub CommandButton1_Click()
    rand = WorksheetFunction.RandBetween(1, 2)
    If rand = 1 Then
        Optminor.Value = True
    Else
        Optmajor.Value = True
    End If
End Sub


Private Sub Optminor_Click()
    ComboBox1.Visible = False
    ComboBox2.Visible = False
    ComboBox3.Visible = False
    ComboBox4.Visible = False
    ComboBox4.Value = ""
    ComboBox4.Clear
    If Optminor Then
        
        ComboBox1.Visible = True
        ComboBox2.Visible = True
        ComboBox4.Visible = True
        
        rand = WorksheetFunction.RandBetween(0, ComboBox1.ListCount - 1)
        ComboBox1.Value = ComboBox1.List(rand)
        rand = WorksheetFunction.RandBetween(0, ComboBox2.ListCount - 1)
        ComboBox2.Value = ComboBox2.List(rand)
        
    End If
End Sub


Private Sub Optmajor_Click()
    ComboBox1.Visible = False
    ComboBox2.Visible = False
    ComboBox3.Visible = False
    ComboBox4.Visible = False
    ComboBox4.Value = ""
    ComboBox4.Clear
    If Optmajor Then
        ComboBox3.Visible = True
        ComboBox4.Visible = True
        
        rand = WorksheetFunction.RandBetween(0, ComboBox3.ListCount - 1)
        ComboBox3.Value = ComboBox3.List(rand)
    
    End If
    
End Sub


Private Sub UserForm_Activate()


    ComboBox1.Visible = False
    ComboBox2.Visible = False
    ComboBox3.Visible = False
    ComboBox4.Visible = False
    
    Set sh = Sheets("[COLOR=#ff0000]sheet1[/COLOR]")
    ComboBox1.List = sh.Range("A2:A" & sh.Range("A" & Rows.Count).End(xlUp).Row).Value
    ComboBox2.List = sh.Range("B2:B" & sh.Range("B" & Rows.Count).End(xlUp).Row).Value
    ComboBox3.List = sh.Range("C2:C" & sh.Range("C" & Rows.Count).End(xlUp).Row).Value


End Sub
 

Forum statistics

Threads
1,141,312
Messages
5,705,682
Members
421,406
Latest member
kluna90

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
Top