Random selection of comboboxes

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
106
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,825
Office Version
2007
Platform
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,082,043
Messages
5,362,836
Members
400,694
Latest member
Sofie17

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top