Simplify code

Emperor

Board Regular
Joined
Mar 25, 2010
Messages
225
Hi all,

I have 10 listboxes in my Userform and want to check if there is at least 1 item selected. I currently use this code, but can this be a loop instead of 10 different pieces of code?

Code:
Dim i As Integer
Dim count As Integer


'Categorie
count = 0
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            count = count + 1
        End If
    Next i
If count = 0 Then
MsgBox ("You have to select at least 1 item!")
Exit Sub
End If

Thanks in advance!

Mathijs.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Code:
[COLOR="Blue"]Sub[/COLOR] AreThereEmptyListBoxes()

    [COLOR="Blue"]Dim[/COLOR] i [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Integer[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] ctl [COLOR="Blue"]As[/COLOR] MSForms.Control
    [COLOR="Blue"]Dim[/COLOR] lb [COLOR="Blue"]As[/COLOR] MSForms.ListBox
    
    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] ctl [COLOR="Blue"]In[/COLOR] UserForm1.Controls
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]TypeOf[/COLOR] ctl [COLOR="Blue"]Is[/COLOR] MSForms.ListBox [COLOR="Blue"]Then[/COLOR]
            lb = ctl
            [COLOR="Blue"]If[/COLOR] lb.ListIndex > -1 [COLOR="Blue"]Then[/COLOR] i = i + 1
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]Next[/COLOR]
    
    [COLOR="Blue"]If[/COLOR] i > 0 [COLOR="Blue"]Then[/COLOR]
        MsgBox i & " listboxes have(s) selected value."
    [COLOR="Blue"]Else[/COLOR]
        MsgBox "There are no listboxes which have selected value."
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Hi Sektor,

You code errors at 'lb = ctl'

If I read you code correctly it checks if there is an selected item in 1 of the listboxes? While I need it to check if there is 1 item selected in each of the listboxes.

Thanks for your help.

p.s.
I am using Excel 2003
 
Upvote 0
Code:
Sub AreThereEmptyListBoxes()

    Dim i As Integer
    Dim ctl As MSForms.Control
    Dim lb As MSForms.ListBox
    
    For Each ctl In UserForm1.Controls
        If TypeOf ctl Is MSForms.ListBox Then
            [B][COLOR="Red"]Set[/COLOR][/B] lb = ctl
            If lb.ListIndex > -1 Then i = i + 1
        End If
    Next
    
    If i > 0 Then
        MsgBox i & " listboxes have(s) selected value."
    Else
        MsgBox "There are no listboxes which have selected value."
    End If

End Sub
 
Upvote 0
Sektor, thanks for your quick reply.

When I run your new code it allways says there are 9 filled listboxes (even when I select nothing)

I my previous post correct?

Gr. Mathijs.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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