Option Explicit
Private Sub CommandButton1_Click()
Dim choices As Integer, pick As Integer, a As Integer
Dim Multply(6) As Single, Answer As Single
'the number next to "Multply(" needs to be the number of listboxes you have. I have 6
For choices = 1 To UBound(Multply) 'will do 1 to (how many listboxes you have)
For pick = 0 To Me.Controls("ListBox" & choices).ListCount - 1 'don't change, this will loop through all the options
With Me.Controls("ListBox" & choices)
If .Selected(pick) Then 'if it's selected then
Multply(choices) = Multply(choices) + Multiplier(choices, pick) 'add it's multiplier to our array
End If
End With
Next pick
If Multply(choices) = 0 Then Multply(choices) = 1 'if they didn't have any selections, make it 1, otherwise zero time anything is zero
Next choices
'this msgbox is just to demonstrate to you how it's obtaining it's answer, you can comment this out when you're done testing
MsgBox "Will multiply these values:" & vbCrLf & _
Multply(1) & vbCrLf & _
Multply(2) & vbCrLf & _
Multply(3) & vbCrLf & _
Multply(4) & vbCrLf & _
Multply(5) & vbCrLf & _
Multply(6)
Answer = 1 'answet MUST start out as 1, otherwise, it'll be a zero value and zero times anything = zero, lol
For a = 1 To UBound(Multply) 'will loop through the array and multply it out
Answer = Multply(a) * Answer
Next a
Label1.Caption = Answer 'shows you the answer :-)
End Sub
Private Function Multiplier(whichList As Integer, whichSelection As Integer) As Single
Select Case whichList
'this outer bunch of cases needs to have 1 case for each listbox
'this example uses 6 so i have 6 outer cases
Case 1: 'options in first listbox
Select Case whichSelection + 1
'these inner bunch of cases, need 1 for each possible selection for that box
Case 1: Multiplier = 1 'set these numbers to your multipliers
Case 2: Multiplier = 2 'make sure they're in the same order as the ones you added
Case 3: Multiplier = 3
End Select
Case 2: 'options in second listbox
Select Case whichSelection + 1
'box 2 has 4 options, so 4 cases here
Case 1: Multiplier = 1
Case 2: Multiplier = 2
Case 3: Multiplier = 3
Case 4: Multiplier = 4
End Select
Case 3: 'options in third listbox
Select Case whichSelection + 1
Case 1: Multiplier = 1
Case 2: Multiplier = 2
Case 3: Multiplier = 3
End Select
Case 4: 'options in fourth listbox
Select Case whichSelection + 1
Case 1: Multiplier = 1
Case 2: Multiplier = 2
Case 3: Multiplier = 3
End Select
Case 5: 'options in fifth listbox
Select Case whichSelection + 1
'box 5 has 4 options (.additem was done 4 times)
Case 1: Multiplier = 1
Case 2: Multiplier = 2
Case 3: Multiplier = 3
Case 4: Multiplier = 4
End Select
Case 6: 'options in sixth listbox
Select Case whichSelection + 1
Case 1: Multiplier = 1
Case 2: Multiplier = 2
Case 3: Multiplier = 3
End Select
End Select
End Function
Private Sub CommandButton2_Click()
Unload Me 'just exits this thing for you
End Sub
Private Sub UserForm_Initialize()
CommandButton1.Caption = "Calculate"
CommandButton2.Caption = "Done"
Label1.Font.Size = 20
Label1.Caption = "answer"
'this enables multiple selections on the listboxes, you can delete these lines if you only want them _
to be able to pick 1 option. Or delete them if you set these variables in advance with the properties window
ListBox1.MultiSelect = fmMultiSelectMulti
ListBox2.MultiSelect = fmMultiSelectMulti
ListBox3.MultiSelect = fmMultiSelectMulti
ListBox4.MultiSelect = fmMultiSelectMulti
ListBox5.MultiSelect = fmMultiSelectMulti
ListBox6.MultiSelect = fmMultiSelectMulti
'this adds all the possible options to the listboxes
'you can add as many as you want, just make sure your multipliers in the function up above are in the SAME ORDER
'in the multiplier function above, your values must match the order of these
ListBox1.AddItem "#1, option 1" 'multiplier Case 1: Case 1:
ListBox1.AddItem "#1, option 2" 'multiplier Case 1: Case 2:
ListBox1.AddItem "#1, option 3" 'multiplier Case 1: Case 3:
ListBox2.AddItem "#2, option 1" 'multiplier Case 2: Case 1:
ListBox2.AddItem "#2, option 2" 'you get the idea...
ListBox2.AddItem "#2, option 3"
ListBox2.AddItem "#2, option 4"
ListBox3.AddItem "#3, option 1"
ListBox3.AddItem "#3, option 2"
ListBox3.AddItem "#3, option 3"
ListBox4.AddItem "#4, option 1"
ListBox4.AddItem "#4, option 2"
ListBox4.AddItem "#4, option 3"
ListBox5.AddItem "#5, option 1"
ListBox5.AddItem "#5, option 2"
ListBox5.AddItem "#5, option 3"
ListBox5.AddItem "#5, option 4"
ListBox6.AddItem "#6, option 1"
ListBox6.AddItem "#6, option 2"
ListBox6.AddItem "#6, option 3"
End Sub