Consulting

Results 1 to 17 of 17

Thread: Solved: listbox little bug

  1. #1

    Solved: listbox little bug

    Hi I have a userform, which once completed, fills in a worksheet and then gets emailed to 'someone' (user defined)

    I think i've got it cracked, but one thing won't work

    I have a 2 listboxes, Listbox1 is filled with a range from another sheet, Listbox2 gets filled by highlighting an item from Listbox1 and transferring it with a command button.

    when i copy the form using
    .Offset(RowCount, 10).Value = Me.ListBox2.List
    It only returns the first item

    Can anyone help?

  2. #2
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Are you trying to return all selected items in ListBox2 to a range? I think you need to loop thru the list box, something like:

    [vba]
    For lItem = 0 To Me.ListBox2.ListCount - 1 '// start at zero, not one //
    If Me.ListBox2.Selected(lItem) Then
    .Range("J" & RowCount) = Me.ListBox2.List(lItem)
    RowCount = RowCount + 1
    End If
    Next lItem
    [/vba]
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  3. #3
    Not sure

    I'm trying to fill a cell in a sheet with the contents of listbox2

    so if Listbox2 contains
    apples
    pears
    bananas

    The cell on the sheet contains
    apples
    pears
    bananas

  4. #4
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Just to be clear, you want one cell to contain all selected items in ListBox2? Are they separated with a carriage return?
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  5. #5
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Something like this maybe...

    [VBA]Sub ListOneCell()
    Dim i As Long
    Dim x As Variant


    For i = 0 To Me.ListBox1.ListCount - 1
    x = x + Me.ListBox1.List(i) & vbNewLine
    Next i

    Range("A1").Value = x

    End Sub[/VBA]

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    .Offset(Rowcount, 10).Resize(Me.ListBox2.ListCount) = Me.ListBox2.List
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    This will not add an extra line at the bottom like my previous code.

    [vba]Sub ListOneCell()
    Dim i As Long
    Dim x As Variant

    For i = 0 To Me.ListBox1.ListCount - 1
    If i <> Me.ListBox1.ListCount - 1 Then
    x = x + Me.ListBox1.List(i) & vbNewLine
    Else
    x = x + Me.ListBox1.List(i)
    End If
    Next i

    Range("A1").Value = x

    End Sub[/vba]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  8. #8
    yep, that's correct, one cell to contain all values

    The listbox2 has carriage returns (I believe)

  9. #9
    I will try that code thanks

    Where would it go?

    I have a submit command button, which when pressed, completes the sheet with all the userform data

    Can this code go in that?

  10. #10
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Yes without the Sub/End Sub lines

    Like this...

    [VBA]Private Sub CommandButton1_Click()
    Dim i As Long
    Dim x As Variant

    For i = 0 To Me.ListBox1.ListCount - 1
    If i <> Me.ListBox1.ListCount - 1 Then
    x = x + Me.ListBox1.List(i) & vbNewLine
    Else
    x = x + Me.ListBox1.List(i)
    End If
    Next i

    Range("A1").Value = x

    End Sub[/VBA]
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  11. #11
    xld
    tried that one (seemed the simplest!) It works, but it adds each item into a new cell - I need them all in one cell. Can this code be modified?

    A normal textbox will return all the values, including carriage returns and multi-line. Obviously a listbox is more tricky?

  12. #12
    thanks georgiboy

    one thing though - how does the code know which cell to put the listbox items into?

    thanks

  13. #13
    hoorah
    worked it out myself - aren't I cleverl!

    changed range("A1") to equal my offest cell value

    and it works!

    brilliant

    Thanks again

  14. #14
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    This part at the end...

    Range("A1").Value = x

    ...is placing the value of x into range A1

    Your range is...

    .Offset(RowCount, 10).Value

    ...so it should look like...

    .Offset(RowCount, 10).Value = x

    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by elsteshepard
    xld
    tried that one (seemed the simplest!) It works, but it adds each item into a new cell - I need them all in one cell. Can this code be modified?

    A normal textbox will return all the values, including carriage returns and multi-line. Obviously a listbox is more tricky?
    [vba]

    .Offset(Rowcount, 10).Resize(Me.ListBox2.ListCount).Value = Me.ListBox2.List
    .Offset(Rowcount, 10).Value = Join(Application.Transpose(.Offset(Rowcount, 10).Resize(Me.ListBox2.ListCount)), ",")
    .Offset(Rowcount + 1, 10).Resize(Me.ListBox2.ListCount - 1).ClearContents
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    brilliant, and it doens't have carriage returns either, even better

    but...there's always a but
    Listbox2 might actually be blank
    if it is, i get an error

    run-time error '1004'
    application defined or object defined erro

    i'm guessing i need some kind of if statement?

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    If Me.ListBox2.ListCount > 0 Then

    With .Offset(Rowcount, 10)

    .Resize(Me.ListBox2.ListCount).Value = Me.ListBox2.List
    .Value = Join(Application.Transpose(.Resize(Me.ListBox2.ListCount)), ",")
    .Offset(1, 0).Resize(Me.ListBox2.ListCount - 1).ClearContents
    End With
    End If
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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