UserForm Listbox selections

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,342
Office Version
  1. 365
Platform
  1. Windows
I have code that allows the users to select multiple rows in a list box and it puts those selections on a sheet starting in B11
It is taking the value from column 10 of the Listbox

Is there a way to modify this code so that it pastes both the values from columns 10 & 11?


Code:
Private Sub CommandButton1_Click()


ListBox3.MultiSelect = fmMultiSelectMulti

'Loop through every item in the ListBox
For i = 0 To ListBox3.ListCount - 1

    'Check if the item was selected.
    If ListBox3.Selected(i) Then

        'If here, means this item has been selected.

        'Put all selected items in Column B
 With Sheets("Selected Tasks")
        If Len(.Range("B11").Value) = 0 Then
            .Range("B11").Value = ListBox3.List(i, 11)
        Else
            .Range("B" & .Rows.Count).End(xlUp).Offset(1).Value = ListBox3.List(i, 11)
        End If
    End With


    End If

Next i


UserForm3.Hide



End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Just to clear one thing up, you are not taking data from column 10. You are taking data from the column at index 11, which is the 12th column (first column is at index 0).

Do you want to combine those two values in one cell? Or do you want a value in column B and a value in column C?

Combine them in one cell like this:

VBA Code:
        If Len(.Range("B11").Value) = 0 Then
            .Range("B11").Value = ListBox3.List(i, 11) & ListBox3.List(i, 12)
        Else
            .Range("B" & .Rows.Count).End(xlUp).Offset(1).Value = ListBox3.List(i, 11) & ListBox3.List(i, 12)
        End If

or two columns like this:

VBA Code:
        If Len(.Range("B11").Value) = 0 Then
            .Range("B11").Resize(1, 2).Value = Array(ListBox3.List(i, 11), ListBox3.List(i, 12))
        Else
            .Range("B" & .Rows.Count).End(xlUp).Offset(1).Resize(1, 2).Value = Array(ListBox3.List(i, 11), ListBox3.List(i, 12))
        End If
 
Upvote 0
This will store the data from columns 11 and 12 of the selected rows in an array before writing to the sheet.
VBA Code:
Private Sub CommandButton1_Click()
Dim rngDst As Range
Dim arrSelected As Variant
Dim cnt As Long
Dim idx As Long

    With Me.ListBox3
        ReDim arrSelected(1 To 2, 1 To .ListCount + 1)
        For idx = 0 To .ListCount - 1
            If .Selected(idx) Then
                cnt = cnt + 1
                arrSelected(1, cnt) = .List(idx, 10)
                arrSelected(2, cnt) = .List(idx, 11)
            End If
        Next idx
    End With

    If idx > 0 Then
        ReDim Preserve arrSelected(1 To 2, 1 To cnt)
        With Sheets("Selected Tasks")
            If .Range("B11").Value = 0 Then
                Set rngDst = Range("B11")
            Else
                Set rngDst = .Range("B" & Rows.Count).End(xlUp).Offset(1)
            End If
        End With
        rngDst.Resize(cnt, 2).Value = Application.Transpose(arrSelected)
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,752
Messages
6,126,672
Members
449,327
Latest member
John4520

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