Bring all data from a lstbox to a cell

G

gatarossi

Dear all,

I have a listbox in a form, and I need to bring all the data in this
listbox in a cell... How can I do it?

lstTest

Item 1
Item 2
Item 3

So when I click in a button in this form, the cell (1,1) needs to
have
all the data: Item 1, Item 2, Item3

I input this code in the button, but it brings only the first item in
the lstTest...


Cells(1, 1).Value = lstTest.List


Thanks in advance!!!


André.
 
B

Bernie Deitrick

Andre,

I am assuming your listbox is on a userform.

You either need to write the data to the same number of cells as items:

Cells(1, 1).Resize(lstTest.ListCount).Value = ListBox1.List

or you need to create a string with all the values:

Dim myStr As String
myStr = lstTest.List(0)
For i = 1 To lstTest.ListCount - 1
myStr = myStr & "," & lstTest.List(i)
Next i
Cells(1, 1).Value = myStr

Your choice.

HTH,
Bernie
MS Excel MVP


Dear all,

I have a listbox in a form, and I need to bring all the data in this
listbox in a cell... How can I do it?

lstTest

Item 1
Item 2
Item 3

So when I click in a button in this form, the cell (1,1) needs to
have
all the data: Item 1, Item 2, Item3

I input this code in the button, but it brings only the first item in
the lstTest...


Cells(1, 1).Value = lstTest.List


Thanks in advance!!!


André.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top