VBA code for selected item in listbox

HYKE

Active Member
Joined
Jan 31, 2010
Messages
373
Hi,

Found this code in the net, what it does reflect the selected item in the msgbox, however this codes takes only the 1st column, how can this code be modified to give the 2nd column and 3rd column data in the msgbox..

Thanks again to all of you for the help..
Code:
    Dim ColCnt As Integer
    Dim rng As Range
    Dim cw As String
    Dim c As Integer
    
    ColCnt = ActiveSheet.UsedRange.Columns.Count
    Set rng = ActiveSheet.UsedRange
    With ListBox1
        .ColumnCount = ColCnt
        .RowSource = rng.Address
        cw = ""
        For c = 1 To .ColumnCount
            cw = cw & rng.Columns(c).Width & ";"
        Next c
        .ColumnWidths = cw
        .ListIndex = 0
    End With
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Have you posted the right code?

That code is for populating the listbox, not displaying the selected item from the listbox.

Anyway, here's how you can do what you want.
Code:
Dim I As Long
Dim msg As String
 
    If ListBox1.ListIndex <> -1 Then
        For I = 0 To ListBox1.ColumnCount - 1
            msg = msg & ListBox1.Column(I) & vbCrLf
        Next I
    End If

    MsgBox msg
 
Upvote 0
Hi Norie,

ooppps,I posted the wrong code.

this is the right one
Code:
Dim Msg As String
Dim i As Integer
    If ListBox1.ListIndex = -1 Then
        Msg = "Nothing"
    Else
        Msg = ""
        For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) Then _
              Msg = Msg & ListBox1.List(i) & vbCrLf
        Next i
    End If
 
Upvote 0
Hi Norie,

Thank you for your code. But what about if my selection in listbox is multiple?
Can this be possible?

Thanks!
 
Upvote 0
Is your listbox multiselect?

The first part of that code seems to be treating as being single select and the second part treating it as though it was multiselect.
 
Upvote 0
Hi Norie,

In the userform are 3 options for the user(codes below)
Code:
Private Sub obExtend_Click()
    ListBox1.MultiSelect = fmMultiSelectExtended
End Sub
Private Sub obMulti_Click()
    ListBox1.MultiSelect = fmMultiSelectMulti
End Sub
Private Sub obSingle_Click()
    ListBox1.MultiSelect = fmMultiSelectSingle
End Sub

but I prefer multi select as the selection gets transferred to the worksheet.
The code below is for transferring.
Code:
Dim lngItem As Long
Dim I As Long
Dim msg As String
 
    If ListBox1.ListIndex <> -1 Then
        For I = 0 To ListBox1.ColumnCount - 1
            msg = msg & ListBox1.Column(I) & vbCrLf
        Next I
    End If

    MsgBox msg
    For lngItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lngItem) Then
            With Sheets("PCA") '< qualify sheet here
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Value = ListBox1.List(lngItem, 1)
                .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = ListBox1.List(lngItem, 2)
            End With
        End If
    Next lngItem
 
Upvote 0
If it is a multiselect listbox you don't need this:
Code:
 If ListBox1.ListIndex = -1 Then
Try this.
Code:
Dim I As Long
Dim J As Long
Dim msg As String
Dim arrItems() As String

    ReDim arrItems(0 To ListBox1.ColumnCount - 1)
    For J = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(J) Then

            For I = 0 To ListBox1.ColumnCount - 1
                arrItems(I) = ListBox1.Column(I, J)
            Next I
            msg = msg & Join(arrItems, ",") & vbCrLf & vbCrLf
        End If
    Next J
    MsgBox msg

Here's code to put the data on a worksheet.
Code:
Dim I As Long
Dim J As Long
Dim arrItems()
    ReDim arrItems(0 To ListBox1.ColumnCount - 1)
    For J = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(J) Then

            For I = 0 To ListBox1.ColumnCount - 1
                arrItems(I) = ListBox1.Column(I, J)
            Next I
            
            With Sheets("PCA")
                .Cells(.Rows.Count, "B").End(xlUp).Offset(1).Resize(, ListBox1.ColumnCount).Value = arrItems
            End With
        End If
    Next J
 
Last edited:
Upvote 0
@Norie, may I ask how can I change your code to put the data on a worksheet, but the data will return in a row instead of a column
 
Upvote 0
Hi Norie,
after many days looking and trying to find a solution to my problem I found this code of yours!
I do have also a listbox and your code is working fantastic!
However the issue I have is how can I add more textbox values or combobox values in the worksheet as well..

For example if I have a listbox with several employees and other data in textbox or combobox I like those also appear in the worksheet.
Maybe I have more luck with your code and help on this?

Would be very much appreciated!

Albert
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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