Solved

Populating a multi-column listbox from another list box

Posted on 2014-11-18
9
1,495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 52

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 52

Expert Comment

by:Rgonzo1971
ID: 40450437
at which line?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:GPSPOW
ID: 40450516
The error is:

Run-time error '6'

Overflow
0
 
LVL 48

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 48

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

630 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