Help Needed for user form command button

freeisgood

New Member
Joined
Jan 18, 2008
Messages
31
Hello All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Codes below is for a command button of a user form (codes found on the web), for each execution of this command, items selected by users is separated by a comma. My goal is to have them separate by a break line. I added “char(10)” to line 6 "strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem)". It works, with the exception that it start by a break line then entry selected by users. <o:p></o:p>
<o:p></o:p>
I am seeking help to make the necessary adjustment to remove the unnecessary break line (first one only). I am learning VBA and a quick explanation would be greatly appreciated.<o:p></o:p>
<o:p></o:p>
Code:<o:p></o:p>
Private Sub CommandButton1_Click()<o:p></o:p>
<o:p></o:p>
Dim lngItem As Long, strItems<o:p></o:p>
<o:p></o:p>
With ListBox1<o:p></o:p>
For lngItem = 0 To ListBox1.ListCount - 1<o:p></o:p>
If ListBox1.Selected(lngItem) Then<o:p></o:p>
strItems = strItems & "," & Chr(10) & ListBox1.List(lngItem) – 1<o:p></o:p>
<o:p></o:p>
End If<o:p></o:p>
Next lngItem<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
ActiveCell.Value = Replace(strItems, ",", "", 1, 1)<o:p></o:p>
<o:p></o:p>
Unload Me<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
Regards<o:p></o:p>
VL<o:p></o:p>
 
Try this
Code:
Private Sub CommandButton1_Click()
    Dim lngItem As Long, strItems As String

    strItems = ""

    With ListBox1
        For lngItem = 0 To ListBox1.ListCount - 1   
            strItems & Chr(10) & ListBox1.List(lngItem) – 1
        Next lngItem
    End With
    
    strItems = Mid(strItems, 2): Rem remove first chr(10)

    ActiveCell.Value = strItems

    Unload Me

End Sub
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi Mike,

That was more of the results I was expecting.

Needed a small adj.:

strItems = strItems & Chr(10) & ListBox1.List(lngItem)

-Jeff
 
Upvote 0
What did I do wrong? Even with suggested adjustment by Repairman615 still does not work.

Codes:

Private Sub CommandButton1_Click()
Dim lngItem As Long, strItems As String
strItems = ""
With ListBox1
For lngItem = 0 To ListBox1.ListCount - 1
strItems = strItems & Chr(10) & ListBox1.List(lngItem)
Next lngItem
End With
strItems = Mid(strItems, 2): Rem remove first chr(10)
ActiveCell.Value = strItems
Unload Me
End Sub
 
Upvote 0
What "doesn't work"?
Does nothing go into the cell? Or does the wrong cell go into the cell? or

Perhaps

Code:
Private Sub CommandButton1_Click()
    Dim lngItem As Long, strItems As String
    strItems = ""
    With ListBox1
        For lngItem = 0 To .ListCount - 1
            If .Selected(lngItem) Then strItems = strItems & Chr(10) & ListBox1.List(lngItem)
        Next lngItem
    End With

    strItems = Mid(strItems, 2): Rem remove first chr(10)
    ActiveCell.Value = strItems

    Unload Me
End Sub
 
Upvote 0
You could try an array, something like this perhaps.
Code:
Option Explicit
 
Private Sub CommandButton1_Click()
Dim lngItem As Long, strItems As String
Dim arrItems()

    With ListBox1
        For lngItem = 0 To ListBox1.ListCount - 1
            ReDim Preserve arrItems(lngItem)
            arrItems(lngItem) = ListBox1.List(lngItem)
        Next lngItem
    End With

    strItems = Join(arrItems, Chr(10))
 
    ActiveCell.Value = strItems

    Unload Me

End Sub
 
Upvote 0
Thanks Mikerickson,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
I misunderstood Repairman615’s instruction, now it works.<o:p></o:p>
<o:p> </o:p>
Again thanks all!<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top