copy values from listbox to worksheet

ahmad123

New Member
Joined
May 24, 2011
Messages
11
Helloooo,

I have a multi selction listbox, i need to copy all the values that i selected in this list box into a column in a worksheet :

1- Select all the values from the list
2- Click on a button
3- Unload the listbox and get the selectd values in in column "A"

Is there a way to do so ??

Thanx for any input
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try like this

Code:
For i = 1 To Me.ListBox1.ListCount
  Cells(i + 1, 1) = Me.ListBox1.List(i - 1)
Next i
 
Upvote 0
Hi VoG,

this is copying all the values in the list and pasting it in the right column. I need to copy the selected values only (the highlighted ones)

Thx in advance
 
Upvote 0
Maybe

Code:
For i = 1 To Me.ListBox1.ListCount
  If Me.ListBox1.Selection(i).Selected = True Then
  j = j + 1
  Cells(j, 1) = Me.ListBox1.List(i - 1)
Next i
 
Upvote 0
I am getting "argument not optionnal" compile error, for ".selected".

Any ideas on how to fix that ??
 
Upvote 0
Try this:
Rich (BB code):

' Code of UserForm1 module
Const SourseAddr$ = "F1:I10"
Const DestAddr$ = "A1"

Private Sub UserForm_Initialize()
  Dim a
  ' Populate ListBox1
  a = Range(SourseAddr).Value
  With ListBox1
    .ColumnCount = UBound(a, 2)
    .List = a
  End With
End Sub

Private Sub CommandButton1_Click()
  Dim a, i&, r&, c&
  With ListBox1
    a = .List
    For r = 0 To UBound(a)
      If .Selected(r) Then
        For c = 0 To UBound(a, 2)
          a(i, c) = .List(r, c)
        Next
        i = i + 1
      End If
    Next
  End With
  If i Then
    ' Copy selected rows to destination
    Range(DestAddr).Resize(i, UBound(a, 2) + 1) = a
    MsgBox "Copied  items: " & i, vbInformation
    Unload Me
  Else
    MsgBox "Items are not selected", vbExclamation 
  End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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