Select and Copy Listbox Items

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Select and Copy Listbox Items

Post by adam »

in the attached workbook; the buttons select all and run works if the list box has one column. but It does not work with two columns.

How could I make it work with two columns? When the user clicks the select all button it will select all the rows and when the user clicks the run button it will copy comma-separated serial values to textbox2 and comma-separated name values to textbox 3

any help on this would be kindly appreciated.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Select and Copy Listbox Items

Post by adam »

The following modification works

Code: Select all

Private Sub CommandButton5_Click()
Call CallSerial
Call CallName
End Sub

Code: Select all

Private Sub cbSelectAll_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
ListBox1.Selected(i) = True
Next i
End Sub

Code: Select all

Sub CallSerial()
Dim x As Integer
Dim myVar As String
myVar = ""

For x = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(x) Then
        If myVar = "" Then
            myVar = Me.ListBox1.List(x, 0)
        Else
            myVar = myVar & ", " & Me.ListBox1.List(x, 0)
        End If
    End If
Next x

TextBox2 = myVar
End Sub

Code: Select all

Sub CallName()
Dim x As Integer
Dim myVar As String
myVar = ""

For x = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(x) Then
        If myVar = "" Then
            myVar = Me.ListBox1.List(x, 1)
        Else
            myVar = myVar & ", " & Me.ListBox1.List(x, 1)
        End If
    End If
Next x

TextBox3 = myVar
End Sub
Best Regards,
Adam