Listbox / Combobox No Selection

TonySondergeld

New Member
Joined
Jul 11, 2009
Messages
31
If you have a list box on a user form and the user doesn't make a selection it will display message with VbYesNO. if the user then click VbYes then the macro stop and allows the users to make a section/s without closing the user form or if VbNo then Exit the sub This is run with a command button click.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It looks like your listbox allows more than one selection. If so, maybe something like this might suffice...

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] bSelected [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR]
    [COLOR=darkblue]With[/COLOR] Me.ListBox1
        [COLOR=darkblue]For[/COLOR] i = 0 [COLOR=darkblue]To[/COLOR] .ListCount - 1
            [COLOR=darkblue]If[/COLOR] .Selected(i) [COLOR=darkblue]Then[/COLOR]
                bSelected = [COLOR=darkblue]True[/COLOR]
                [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] bSelected [COLOR=darkblue]Then[/COLOR]
        MsgBox "Please make one or more selections, and try again.", vbExclamation
        [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If
    [/COLOR][COLOR=#008000]'etc
    '
    '
    '[/COLOR][COLOR=darkblue][/COLOR]
End [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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