MsgBox linked to command button and listbox

Jazz10

New Member
Joined
Oct 6, 2009
Messages
31
I have a UserForm with a ListBox and a CommandButton. I would like to add a code to the UserForm so if I click the CommandButton and the number of items in the ListBox is less than 3 a message box appears that reads "must add another entry". Here is the code I have right now. If you are able to help, thank you!

Code:
[FONT=Times New Roman][SIZE=3]Private Sub CommandButton1_Click()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]MsgBox "Must add another entry"[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try

Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then j = j + 1
Next i
If j < 3 Then MsgBox "Must add another entry"
End Sub
 
Upvote 0
VoG, this is awesome! Thank you for your help. It works great. I have another macro that I would like to run if the number is greater than or equal to 3 that I should have specified. Below is what I added to your macro but it does not seem to work. Should I open another thread or can you help me here?

Code:
[SIZE=3][FONT=Times New Roman]Private Sub CommandButton1_Click()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Dim i As Long, j As Long<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]For i = 0 To ListBox1.ListCount - 1<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]   If ListBox1.Selected(i) Then j = j + 1<o:p></o:p>[/FONT][/SIZE]
[SIZE=3][FONT=Times New Roman]Next i<o:p></o:p>[/FONT][/SIZE]
[FONT=Times New Roman][SIZE=3]If j < 3 Then MsgBox "Must add another entry"[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If j >=3 Then Call Macro2[/SIZE][/FONT]
<o:p></o:p>
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
 
Upvote 0
Try

Code:
Private Sub CommandButton1_Click()
Dim i As Long, j As Long
For i = 0 To ListBox1.ListCount - 1
   If ListBox1.Selected(i) Then j = j + 1
Next i
If j < 3 Then
    MsgBox "Must add another entry"
Else
    Call Macro2
End If
End Sub
 
Upvote 0
SOLVED: MsgBox linked to command button and listbox

Absolutely Perfect! Thank you for your help!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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