I think I need some advice on forms, please.

cornflakegirl

Well-known Member
Joined
Nov 4, 2004
Messages
2,023
I need to calculate the profit impact of system downtime. The impact is dependent on a number of things, including the length of time, the purchasing channels affected and the products affected.

I want to produce a little spreadsheet that anyone can use that will generate the cost for any set of circumstances. So they need to be able to choose the channel - 1 or more of about 10, and the product - 1 or more of 5. There will be another couple of categories too, with maybe 5 options in each.

Each category option will have have a multiplication factor associated with it - if more than one option is selected, the factors will be summed.

I think I probably need checkboxes, but they seem quite fiddly to set up. Is there a better way?
 
here's the same thing but with a bunch of comments so it's easier to follow for a beginner (no offense)

Code:
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

i made a couple really minor changes to it so it's easier too.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
This is fabulous - thank you so much! Going to go and work through it. And thank you for the comments - I never take offence when someone wants to help me learn!
 
Upvote 0
cool. let me know if it's working the way you wanted / expected. I'm SO glad i could help. i love that feeling!

WARNING, if you rename the listboxes away from their defaults, you'll get an error with my code as it relies on those names to loop through them easily.

also, make sure if you add/delete listboxes, you end up with a sequence still and don't skip any numbers (as in, listbox1, listbox2, listbox4, listbox5) listbox3 needs to be there too.

other then that, i think you'll manage =)
 
Upvote 0

Forum statistics

Threads
1,225,204
Messages
6,183,577
Members
453,170
Latest member
sameer98

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