Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Populating a multi-column listbox from another list box

Posted on 2014-11-18
9
Medium Priority
?
1,740 Views
Last Modified: 2014-11-19
I have two list boxes in an Excel User Form.  I want the user to select an item from ListBox1 and copy the data to ListBox2.

ListBox1 and ListBox2 have identical number of columns, 2 and one bound column.

When I had only one column of data, the command button to move the selected item from ListBox1 to ListBox2 worked:

Private Sub CommandButton3_Click()
    Dim i As Integer
   
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) = True Then ListBox2.AddItem ListBox1.List(i)
    Next i
   
End Sub

What do I need to change in the above VBA to move both columns?

Thank you

Glen
0
Comment
Question by:GPSPOW
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40450371
Hi,

pls try

    For i = 0 To ListBox1.ListCount - 1
         If ListBox1.Selected(i) = True Then 
              ListBox2.AddItem ListBox1.List(i, 0)
              ListBox2.List(ListBox2.ListCount - 1, 1) = ListBox1.List(i, 1)
         End If
     Next i

Open in new window

Regards
1
 

Author Comment

by:GPSPOW
ID: 40450419
I am getting an overflow error
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40450437
at which line?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:GPSPOW
ID: 40450516
The error is:

Run-time error '6'

Overflow
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40450730
Rgonzo1971's code should work just fine. What line of code causes the error?

Do you know how to use Debug? If you don't, or even if you do, you may want to look at this article of mine.
0
 

Author Comment

by:GPSPOW
ID: 40450996
I figured out that the overflow error is due to the ListBox table I am using.  It gets its information from a MS-Query that creates a table.

When I copy the column data to another sheet and use that data in my listbox, I do not get the overflow error.

Any thoughts on how I can correct the data in the Query Table so it does not cause the overflow error?
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 40451006
Can you show the code that you execute when you use the "ListBox table"? And in that code indicate the line that causes the problem?
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 40451763
I suspect your Integer is the problem - try using Long instead.

Not for points, but I'd suggest a more generic approach rather than specifying the number of columns:

Private Sub CommandButton3_Click()
    Dim i                     As Long
    Dim j                     As Long

    ListBox2.ColumnCount = ListBox1.ColumnCount
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            With ListBox2
                .AddItem
                For j = 0 To .ColumnCount - 1
                    .List(.ListCount - 1, j) = ListBox1.List(i, j)
                Next j
            End With
        End If
    Next i

End Sub

Open in new window

1
 

Author Closing Comment

by:GPSPOW
ID: 40452653
Thank you   Worked perfectly
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question