Excel VBA code to copy value in listbox column 1 only of selected row

buroh

New Member
Joined
Jul 14, 2020
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a userform with Listbox1 which shows values from column range (DX20:DY170).

The listbox has two columns.

Column 1 is document number (see below)
Column 2 is description (see below)

1631801214090.png


So what I am looking to do is to select one or more rows in the list box and click on commandbutton1 which will run vba code to copy the values in column 1 only to clipboard.

Then I can manually paste the copied data into another worksheet or word document.

I got code from another post but as it isn't related to my userform listbox, It didn't work as expected as when I clicked on command button with the code and then pasted the copied data into a word document it pasted the values as ￿￿, so not actual text.

Could anyone please help me with this?

let me know what you need to help me with this code.

Userform1
Listbox1

thank you.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Someone might come along and help out but in the meantime - can you share what code you have so far then we/I can try and see where you are going wrong.

Thanks
 
Upvote 0
Someone might come along and help out but in the meantime - can you share what code you have so far then we/I can try and see where you are going wrong.

Thanks
below is what I copied from another thread, so I did originally look for threads to help me, but unfortunately I tried to tweak it for my use but I was unsuccessful.

Dim MyData As DataObject, i As Long, strRow As String
Const strSeparator As String = ", "
With Me.ListBox1
For i = 0 To .ColumnCount - 1
strRow = strRow & .List(.ListIndex, i) & strSeparator
Next i
strRow = Left(strRow, Len(strRow) - Len(strSeparator))
End With
Set MyData = New DataObject
MyData.SetText strRow
MyData.PutInClipboard

Thank you.
 
Upvote 0
Your code works for me....

I put it within the userform code and filled my listbox with a couple of items:

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim MyData As DataObject, i As Long, strRow As String
Const strSeparator As String = ", "
With Me.ListBox1
For i = 0 To .ColumnCount - 1
strRow = strRow & .List(.ListIndex, i) & strSeparator
Next i
strRow = Left(strRow, Len(strRow) - Len(strSeparator))
End With
Set MyData = New DataObject
MyData.SetText strRow
MyData.PutInClipboard
End Sub

Private Sub UserForm_Activate()

With ListBox1
.AddItem "Yes"
.AddItem "no"

End With

End Sub

How did you fill your listbox?
 
Upvote 0
Your code works for me....

I put it within the userform code and filled my listbox with a couple of items:

VBA Code:
Option Explicit

Private Sub CommandButton1_Click()
Dim MyData As DataObject, i As Long, strRow As String
Const strSeparator As String = ", "
With Me.ListBox1
For i = 0 To .ColumnCount - 1
strRow = strRow & .List(.ListIndex, i) & strSeparator
Next i
strRow = Left(strRow, Len(strRow) - Len(strSeparator))
End With
Set MyData = New DataObject
MyData.SetText strRow
MyData.PutInClipboard
End Sub

Private Sub UserForm_Activate()

With ListBox1
.AddItem "Yes"
.AddItem "no"

End With

End Sub

How did you fill your listbox?
Hello,

This is how my listbox is populated:

Private Sub UserForm_Initialize()

Dim rngMultiColumn As Range

'Define source range
Set rngMultiColumn = Worksheets("Specs & Reports").Range("DX20:DY170")

With ListBox1
'Populate listbox
.List = rngMultiColumn.Cells.Value
End With

End Sub

so the two columns in listbox1 is being populated from the spreadsheet's columns (range).

Thanks.
 
Upvote 0
Something like this should work:

You'll need to change things around if your listbox/commandbutton isn't "1" but that should be fairly straightforward...


VBA Code:
Private Sub CommandButton1_Click()
Dim lngItem As Long

  Dim objData As New MSForms.DataObject
    Dim strText

For lngItem = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(lngItem) Then
With Worksheets("Specs & Reports")
strText = ListBox1.List(lngItem, 0)

End With
End If
Next lngItem
    objData.SetText strText
    objData.PutInClipboard
    
End Sub


Private Sub UserForm_Initialize()

Dim rngMultiColumn As Range

'Define source range
Set rngMultiColumn = Worksheets("Specs & Reports").Range("DX20:DY170")

With ListBox1
'Populate listbox
.List = rngMultiColumn.Cells.Value
End With

End Sub
 
Upvote 0
Oh and make sure your "columncount" is the listbox properties = the amount of columns you want it to show (i guess you've already done this though).
 
Upvote 0
Oh and make sure your "columncount" is the listbox properties = the amount of columns you want it to show (i guess you've already done this though).
Thanks, but something is not working.

When I paste what is copied into a word document, I get this:
1631808125547.png


could it be the way I have filled the list box use the vba code I am using that is causing this problem?

I pasted just text into the cell that is populating the textbox, and I am still getting the same paste issue of those two box symbols rather than text.

I do have columncount set correctly.

Thanks
 
Upvote 0
It sounds as though you have a problem with explorer windows open. Have a look here XL2BB 2 Squares
 
Upvote 0
It sounds as though you have a problem with explorer windows open. Have a look here XL2BB 2 Squares
Thank you, it was to do with windows explorer being open, but that is going to cause issues, for others who will be using the spreadsheet as most people will have file explorer open. I am not going to always remember to close file explorer to perform the task.

Apart from the obvious issue, is there a fix?
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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