Userform sum of selection of multiple listboxes

dailan

New Member
Joined
Oct 7, 2016
Messages
20
Dear people,

hope that all is well. I desperately need your help. Hoping to at least receive some feedback.

So, below is the code. What I need to do is to have an If statement if the sum of the two selections of the listboxes is smaller/bigger than a certain value.
I suspect it has something to do with there being a multiselect listboxes. To be clear, one should only be able to select one value in each listbox. Hopefully it makes sense what I'm trying to do. The relevant text is marked in red.

Many thanks in advance!

"
Code:
Private Sub UserForm_initialize()


'Clear listbox1
ListBox1.Value = ""

'Clear listbox2
ListBox2.Value = ""

'Clear listbox3
ListBox3.Value = ""

'Clear textbox1
TextBox1.Value = ""


'Fill listbox 1
With ListBox1
    .AddItem "Aff?rsm?ssig skada"
    .AddItem "Ekonomisk & regulatorisk skada"
End With

'Fill ListBox2
With ListBox2
    .AddItem "3"
    .AddItem "2"
    .AddItem "1"
    .AddItem "0"
End With

'Fill listbox3
With ListBox3
    .AddItem "3"
    .AddItem "2"
    .AddItem "1"
    .AddItem "0"
End With



End Sub



Private Sub CommandButton1_Click()


Dim row, startrow, lastrow As Long
Dim i, col As Variant
Dim ws As Worksheet

'make sheet "Incidenter" active
Sheets("Instruktioner").Activate

Set ws = Sheets("Incidenter")
i = row
col = "B"
lastrow = ws.Cells(Rows.Count, col).End(xlUp).row

'to ensure that incidents with small impact receives a message that the incident is not serious enough to be documented and that we remain on the main page
If[COLOR=#ff0000] ListBox2.Value + ListBox3.Value <= "3" Then[/COLOR]
    Sheets("Instruktioner").Activate
    MsgBox ("K?ra du, din incident har noterats och bed?ms inte vara tillr?ckligt seri?s f?r att incidentrapporteras. Kyss mig!")
End If


'to ensure that only serious breaches are added to the incident documentation
If [COLOR=#ff0000]ListBox3.Value + ListBox2.Value >= "4" Then[/COLOR]

'to copy the last row
ws.Rows(lastrow).Copy

'add another row at the bottom
ws.Rows(lastrow + 1).Insert

'To transfer input data into the table
ws.Cells(lastrow + 1, 2).Value = TextBox1.Value
ws.Cells(lastrow + 1, 3).Value = TextBox4.Value
ws.Cells(lastrow + 1, 4).Value = ListBox1.Value
ws.Cells(lastrow + 1, 5).Value = TextBox2.Value
ws.Cells(lastrow + 1, 6).Value = TextBox3.Value
ws.Cells(lastrow + 1, 7).Value = ListBox2.Value
ws.Cells(lastrow + 1, 8).Value = ListBox3.Value
ws.Cells(lastrow + 1, 10).Value = ""
ws.Cells(lastrow + 1, 11).Value = ""

MsgBox ("K?ra du, din incident har dokumenterats. Kyss mig!")

End If

Unload UserForm1
UserForm1.Hide

End Sub
"
 
Last edited by a moderator:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi there. As written, your listboxes and the comparison are all text strings. Try removing the quotes from the value you are comparing it to, and making sure the results are numbers .
So, change your first failing bit of code to: If CInt(ListBox2.Value) + CInt(ListBox3.Value) <= 3 Then
 
Upvote 0
Dear John,

is not all i'm going to reply. It works! A hundred of thanks to you!

Have a nice weekend!

Kr

Daniel
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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