Try this:
Sub CopySelectedItems()
Dim Counter As Integer
Dim ResultStr As String
Dim LB As MSForms.ListBox
Set LB = UserForm2.ListBox1
For Counter = 0 To LB.ListCount - 1
If LB.Selected(Counter) Then
ResultStr = ResultStr & LB.List(Counter) & vbNewLine
End If
Next
If Len(ResultStr) > 0 Then
UserForm1.TextBox1.Text = ResultStr
End If
End Sub
I've assumed you want each item on its own line in the text box. If not get
rid of the "& vbNewLine" and add "& vbTab" or whatever you want. Note that
for the vbNewLine to work you must set the textbox's MultiLine property to
True.
--
Jim Rech
Excel MVP
|
| Thanks for the response Jim.
|
| Unfortunately I'm trying to copy the selected items from the listbox on
| uf2 and display them in a textbox on uf1, not another listbox. I've
| tried tinkering with the code below to see if I could modify it to copy
| it directly to a textbox, but to no avail. Can you suggest how I would
| modify the code to achieve this.
|
| Jim Rech Wrote:
| > I'd suggest that in second userform's module you have a sub like this
| > run
| > from the button's click:
| >
| > Private Sub CommandButton1_Click()
| > CopySelectedItems
| > Unload Me
| > End Sub
| >
| > and in a standard module a routine like this to do the actual "copy" of
| > the
| > selected items:
| >
| > Sub CopySelectedItems()
| > Dim Counter As Integer
| > Dim LB As MSForms.ListBox
| > Set LB = UserForm2.ListBox1
| > For Counter = 0 To LB.ListCount - 1
| > If LB.Selected(Counter) Then
| > UserForm1.ListBox1.AddItem LB.List(Counter)
| > End If
| > Next
| > End Sub
| >
| >
| > --
| > Jim Rech
| > Excel MVP
| >
| > |
|
|
| --
| Alan T
|
|
| ------------------------------------------------------------------------
| Alan T's Profile:
http://www.excelforum.com/member.php?action=getinfo&userid=2068
| View this thread:
http://www.excelforum.com/showthread.php?threadid=268420
|