Consulting

Results 1 to 13 of 13

Thread: Return Multiple Listbox Selection

  1. #1
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location

    Exclamation Return Multiple Listbox Selection

    Hello Everybody,

    First, please don’t ask for me to upload my document, I can’t, its work related and I am not allowed.

    Second, I don’t any code established yet because I cannot seem to find the code I am looking for anywhere, hence me coming here.

    Ok now:

    I am trying to return a multiple item selection in a ListBox to multiple cells in Excel.

    Example:

    Listbox Selection: X, Y, Z

    Returns
    Column A
    1. Header
    2. X
    3. Y
    4. Z
    5.

    I have tried multiple searches asking for return to column, return as array, yet nothing seems to give me an approach by code that seems to work or return what I am looking for.

    Any hints and/or help you can provide would be greatly appreciated!

    Thanks!
    Chris

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
        Sheet1.Cells(1, 1) = "ListBox1 Selected Items"
        For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) = True Then Sheet1.Cells(i + 2, 1) = ListBox1.List(i)
        Next
    Semper in excretia sumus; solum profundum variat.

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Quote Originally Posted by cpounds217 View Post
    First, please don’t ask for me to upload my document, I can’t, its work related and I am not allowed.
    Could you upload your document? All right then, a document? Make one from scratch, with a listbox and button, some dummy data to fill the listbox etc. It will answer many questions at a stroke: Where is the listbox? (On a sheet or a userform.), What kind of listbox is it (ActiveX or not)? Does it have multiple columns, which column(s) do you want to return to the sheet. What/Where is the button or mechanism to start the code running?
    If you're not allowed to upload anything at all from work, do it from home.
    It will benefit you with a quick and accurate answer without our guessing (wrongly) what your setup is, and we won't be left wondering what your setup is like, and a big plus for us, less work to do setting up such a document so we're more inclined to give an answer at all!
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Paulked's code refactored
    Dim Rw as  long
    Dim i As Long
    
    Rw = 1
    Sheet1.Cells(Rw, "A") = "ListBox1 Selected Items" 
    Rw = 2
    
    For i = 0 To ListBox1.ListCount - 1 
        If ListBox1.Selected(i) = True Then Sheet1.Cells(Rw, "A") = ListBox1.List(i) 
    Rw = Rw + 1
    Next
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I think the other suggestions assume that the selected items start at the beginning and are contiguous

    This might be a little more flexible since it uses an output row counter that get incremented only when a selected list box item is written to the sheet

    Dim idxListbox As Long, idxCell As Long
    
    idxCell = 1
    
    Sheet1.Cells(1, idxCell) = "ListBox1 Selected Items"
    
    For idxListbox = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(idxListbox) Then
            idxCell = idxCell + 1
            Sheet1.Cells(i, idxCell) = ListBox1.List(idxListbox)
        End If
    Next
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    Thanks all!

    Now I am trying to return Yes or No for a Checkbox, but only if the checkbox is enabled.

    The Checkbox default is enabled unless the caption of the label for that row of questions is blank:

    If xyzLabel.Caption = “” then
    Checkbox1.Enabled = False
    Elseif xyzLabel.Caption <> “” then
    Checkbox1.Enabled = True
    End if

    That is called in the Userform_Initialize event.

    Currently I have a the following function (ps: I have 25 checkboxes that all have to run and a total of around 250 controls that return values upon clicking enter):
    Private Function YesOrNo (BoxVal as Boolean) As String
    
    Dim ctl As Object
    
    For each ctl in Me.Controls
    if typeof ctl is MsForms.Combobox and ctl.enabled = true then
    If ctl = true then
    YesOrNo = “Yes”
    Elseif ctl = false then
    YesOrNo = “No”
    End if
    End if
    next
    
    End function
    This function is then called in similar fashion for all checkboxes:

    Private Function MoveValues ()

    wsAnalysis.Range(“B2”) = YesOrNo (Impact1)
    etc...

    End Function

    And of course MoveValues is called into the Enter Button sub.

    I currently have it so it stops telling me invalid use of Null which was happening when I tried Triple State. But now I get no value returned at all.

    Please help!
    Last edited by SamT; 01-31-2018 at 02:31 PM.

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    The YesNo Function will always reflect the state of the last ComboBox in the Form's Collection of Controls.

    Further, since a ComboBox can not be True, YesNo will always be "No".
    OR:
    Since a ComboBox can not be false, YesNo will be "Yes" any time that ComboBox is enabled, which is always.

    Regardless, It is a GIGO Function.

    Private Function YesOrNo(Ctl As Object) As String
    YesOrNo = "No"
    if Ctl = True and Ctl.Enabled = True then YesOrNo = "Yes"
    End Function
    Last edited by SamT; 01-31-2018 at 02:51 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    Ok, but this will return Unchecked, Disabled checkboxes as “No” and not as blank, correct?

    I am looking for:
    Checkbox = True = Yes
    Checkbox = False = No
    Checkbox = Disabled = “ “

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Correct.
    Private Function YesOrNo(Ctl As Object) As String 
    'Returns "Yes", "No", or ""
    
    If not Ctl.Enabled then Exit Function
    
    YesOrNo = "No"
    
    If Ctl = True Then YesOrNo = "Yes" 
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  10. #10
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    Thank you! That helped immensely.

  11. #11
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    I now have my values populated as I want them. Now I need those values to compare to two lists (per value) that house the value and a corresponding numerical value. Then all ten numerical values are added for a score. I am using a formula, but I am having difficulty with creating just the right formula for this. I know I have used it before something similar before, but that was when the lists and lookup value were on the same worksheet, currently (and preferably) they are not on the same worksheet.

    Below is the formula I have tried, but it continually returns a “There’s a problem with this formula” message, which doesn’t help because when I click ‘OK’, it highlights the entire formula as the problem. As you can see I am testing whether the first value in the row is “Yes”, and then adding the assigned numerical values that correspond with the text values for the score. The numerical lists are housed in xyzV (after INDEX function) and the text values are housed in xyz.

    I am really lost on what I am doing wrong here, and I have tried numerous google searches on what formulas to use, how to set it up, and nothing seems to return a valid or helpful answer.

    =If(B2="Yes",(SUM(INDEX(OccurV,MATCH(C2,Occur,0))),(INDEX(AnotherV,MATCH(D2 ,Another,0))),(INDEX(FreqV,MATCH(E2,Freq,0))),(INDEX(PopulationV,MATCH(F2,P opulation,0))),(INDEX(PrivateV,MATCH(G2,Private,0))),(INDEX(InfraV,MATCH(H2 ,Infra,0))),(INDEX(WarningV,MATCH(I2,Warning,0))),(INDEX(DurationV,MATCH(J2 ,Duration,0))),(INDEX(OperationsV,MATCH(K2,Operations,0)))),0)

    Thanks!

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    MATCH(F2,P opulation,0)
    Typo?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    VBAX Regular cpounds217's Avatar
    Joined
    Nov 2017
    Location
    Albany
    Posts
    49
    Location
    Oh, I see that now. But I ended up working with VLOOKUP(......)+VLOOKUP(....) etc, and just hid the arrays on the same worksheet.

    Thanks for the help though!

Tags for this Thread

Posting Permissions

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