Solved

Populating a multi-column listbox from another list box

Posted on 2014-11-18
9
1,046 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 48

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
0
 

Author Comment

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

Expert Comment

by:Rgonzo1971
ID: 40450437
at which line?
0
 

Author Comment

by:GPSPOW
ID: 40450516
The error is:

Run-time error '6'

Overflow
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 45

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 45

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 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now