+ Reply to Thread
Results 1 to 7 of 7

Check if any option in listbox checked

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Check if any option in listbox checked

    Hi please help!

    I have userform and muilti-choise listbox in it. At the beginning some option in this listbox is always chosen, but at the end these chance that no options were chosen. How to check that listbox is clear in this case??? listindex won't be -1.

    thanks in advance!!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Check if any option in listbox checked

    Andrew

    You need to loop.
    Dim arrSelected() As Variant
    Dim idx As Long
    Dim I As Long
    
        For I = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(I) Then
                ReDim Preserve arrSelected(idx)
                arrSelected(idx) = ListBox1.List(idx)
                idx = idx + 1
            End If
        Next I
    
        If I > 0 Then
            MsgBox "Selected: " & Join(arrSelected, ", ")
        Else
            MsgBox "No selection."
        End If
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Re: Check if any option in listbox checked

    Beleive it works! thanks!

  4. #4
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Re: Check if any option in listbox checked

    You misstyped:

    Dim arrSelected() As Variant
    Dim idx As Long
    Dim I As Long
    
        idx=0
        For I = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(I) Then
                ReDim Preserve arrSelected(idx)
                arrSelected(idx) = ListBox1.List(idx)
                idx = idx + 1
            End If
        Next I
    
        If idx > 0 Then
            MsgBox "Selected: " & Join(arrSelected, ", ")
        Else
            MsgBox "No selection."
        End If

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Check if any option in listbox checked

    I copied that code directly from the VBE, after I tested it was working.

    So I'm not sure what I 'mistyped'.

    PS When you declare a vairable as Long then it automatically defaults to 0.

  6. #6
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Moscow
    MS-Off Ver
    Excel 2010
    Posts
    291

    Re: Check if any option in listbox checked

    i bet you did ) with I>0 . try on listbox - choose something and then uncheck all. Then run this macro (with I>0). You'll see

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Check if any option in listbox checked

    Oops, you are right I did mix up idx and I when I was writing the code.

    Thought I'd caught them alll though.

    PS You also mistyped, as this isn't needed.
    idx = 0
    Thought it won't do any harm.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1