add 2 columns of listbox from another listbox

Posted on 2014-03-13
Medium Priority
Last Modified: 2014-03-16
vba excel 2010 userform listbox

Trying to loop through listbox1 and add the first 2 columns to listbox2

Listbox1 has 42 columns
Listbox2 has 2 columns

Dim iIndex
    Dim s As Long, j As Long, k As Long

    With ListBox1
        s = .ListIndex

        ListBox2.AddItem .List(s, 0), 0
        ListBox2.AddItem .List(s, 1), 0
        j = ListBox2.ListCount - 1

        For k = 1 To .ColumnCount - 1
            ListBox2.List(j, k) = .List(s, k)
        Next k
    End With

This is only adding one row and not completely right

Question by:fordraiders
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
  • 2
  • 2
LVL 52

Accepted Solution

Rgonzo1971 earned 2000 total points
ID: 39928514

If you want the first 2 of listbox1 in listbox2 pls try

Dim arrListBxList As Variant
arrListBxList = ListBox1.List
ReDim Preserve arrListBxList(UBound(arrListBxList, 1), 1)
ListBox2.List = arrListBxList

Open in new window

As for your code you loop the ListBox1. Columns instead of the Listbox2.Columns and I don't see a loop for the rows


Author Comment

ID: 39928583
rgonzo, worked great.

I have a weird question: i expanded the data needed to , I THINK column 13


Dim arrListBxList As Variant
arrListBxList = ListBox32.List
ReDim Preserve arrListBxList(UBound(arrListBxList, 1), 13)
ListBox52.List = arrListBxList

45 pt;60 pt;0 pt;0 pt;0 pt;0 pt;0 pt;0 pt;0 pt;0 pt;30 pt;0 pt;0 pt

I had to set my column count 13 and the column widths as above..

i need to see, columns 0,1,10...

I dont understand the math of 13 in the code...

Thanks very much !!
LVL 52

Expert Comment

ID: 39928614

They are 0-based array so if you want 13 columns you have to give 12 as parameter on the redim

on your example the 30pt col is the 11th not the 13th

Author Closing Comment

ID: 39933560

Featured Post

Technology Partners: 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 descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

764 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