Solved

add 2 columns of listbox from another listbox

Posted on 2014-03-13
4
257 Views
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

Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
Hi,

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

Regards
0
 
LVL 3

Author Comment

by:fordraiders
Comment Utility
rgonzo, worked great.

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

UserForm2.ListBox52.Clear

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 !!
0
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
Hi

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
Regards
0
 
LVL 3

Author Closing Comment

by:fordraiders
Comment Utility
thanks
0

Featured Post

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.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

763 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

6 Experts available now in Live!

Get 1:1 Help Now