Solved

Determine if more than one item selected in listbox

Posted on 2011-09-29
4
252 Views
Last Modified: 2012-08-13
vba 2003 excel
userform listbox
frmliststyleoption.
multiselectext

I simply trying to determine if I have more than 1 item selected in listbox ?
when pressing a command button


Thanks
fordraiders
Dim Msg As String, h As Integer
    Msg = ""
    With Me.ListBox1
        For h = 0 To .ListCount - 1
            If h > 1 Then
              MsgBox "Multiple Items Selected", vbCritical, "Item Selection"
               Exit Sub
            End If
        Next h
    End With

Open in new window

0
Comment
Question by:fordraiders
  • 2
4 Comments
 
LVL 5

Expert Comment

by:Khalid Mehmood Awan
ID: 36817383
http://www.ozgrid.com/forum/showthread.php?t=17473&page=1

VB-6    http://www.vb6.us/tutorials/indepth-vb6-listbox-tutorial
VB.NET (below)

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ListBox1.SelectedItems.Count > 1 Then

            MessageBox.Show("Multiple selcted")
        Else
            MessageBox.Show("No item or Single item selected")

        End If
    End Sub
End Class

Open in new window

0
 
LVL 3

Author Comment

by:fordraiders
ID: 36817451
vba does not have property "SelectedItems"
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 36890785
You have to loop:
Dim n As Long, Counter as Long

With Me.Listbox1

For n = 0 To .ListCount - 1

   If .Selected(n) Then Counter = Counter + 1
Next n
msgbox Counter
End With

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 36907715
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now