+ Reply to Thread
Results 1 to 4 of 4

Multi-select listbox output.

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Multi-select listbox output.

    Hello!

    I used this to create a userform multi-select listbox to select pets:

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=369

    Then, I used this to output the listbox's selections:
    http://www.ozgrid.com/VBA/multi-select-listbox.htm

    That's as far as I got.

    1) I would like to use the output like I show in H18:

    The pets selected are XXX, XXX, XXX, and XXX.

    2) Instead of referencing Sheet 2 cells A2:A100 in the code, I would rather use the named range for that area
    "Pets" (which refers to A2:A100). That way, I don't have to change the code if the pet list grows past 100.

    Hopefully, this is an easy fix.

    I appreciate your help!

    VR/Lost
    Last edited by leaning; 05-20-2011 at 08:20 AM.

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Multi-select listbox output.

    Try:

    Dim lItem As Long
    Dim varString As String
    
        For lItem = 0 To listbox1.ListCount - 1
            If listbox1.Selected(lItem) = True Then
                If varString = "" Then
                    varString = listbox1.List(lItem)
                Else
                    varString = varString & vbNewLine & listbox1.List(lItem)
                End If
                listbox1.Selected(lItem) = False
            End If
        Next
    
    MsgBox varString

    For a variable size rowsource use:

    =OFFSET(Sheet1!$A$2,0,0,COUNTA(Sheet1!$A$2:$A$14996),1)

    If you must use a named range it's =INDIRECT("Pets") and use COUNTA in the named range.

    Dion

  3. #3
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Multi-select listbox output.

    Mojo,

    I appreciate your help , especially considering the file I uploaded to the original post was the wrong one and had nothing to do with my question. (The correct one is attached now.)

    Here is how it should work:

    a) Click the button.
    b) Select multiple pets.
    c) Use those pets in a concatenate-type sentence (see Sheet 1, cell F18.)

    a) and b) work great. I copied your code, but I messed something up so the output isn't right. I did something wrong somewhere, just not sure where.

    I appreciate whatever you can do to move this along. Thanks!

    Lost
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: Multi-select listbox output.

    Let's try again then. I assume you mean you want the output to look like H18 (not F18).

    Private Sub cmdOkay_Click()
    Dim i As Long, msg As String, Check As String
    
    msg = "The pets selected are: "
    
    'Generate a list of the selected items
    With ListBox1
        For i = 0 To .ListCount - 1
            If .Selected(i) Then
                msg = msg & .List(i) & ", "
            End If
        Next i
    End With
    
    If msg <> "The pets selected are: " Then
        msg = Left$(msg, Len(msg) - 2) & "."
    End If
    
    If msg = "The pets selected are: " Then
        'If nothing was selected, tell user and let them try again
        MsgBox "Nothing was selected!  Please make a selection!"
        Exit Sub
    Else
        'Ask the user if they are happy with their selection(s)
        Check = MsgBox(msg & vbNewLine & _
            vbNewLine & "Are you happy with your selections?", _
            vbYesNo + vbInformation, "Please confirm")
    End If
    
    If Check = vbYes Then
        'Unload the userform since user is happy with selection(s)
        Sheets("Sheet1").Range("H19").Value = msg
        Unload Me
    Else
        'User wants to try again, so clear listbox selections and
        'return user to the userform
        For i = 0 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = False
        Next
    End If
         
    End Sub

+ 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