VBA Visible Range in a UserForm Listbox

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Hi

I've been searching the forums for some vba code which takes a visible range of cells/columns from a worksheet and places them within a Listbox held within a UserForm
The below code appears to do part of that (product_baseline is the spreadsheet, ShowProducts is the ListBox)
Issue 1: The below is it's not visible cells, it copies everything? any ideas how to apply the .SpecialCells(12) logic to the below?
Issue 2: Appears to cap at 10 columns, if i add a further "ShowProducts.List(i - 1, 10) = .Cells(i, 11).Value" to the code it fails with an could not set the list property value error?


VBA Code:
Dim cell As Range
Dim MyArr  As Variant, i As Long

' intialize array to high number of elements at start

ShowProducts.ColumnWidths = "38.25;80.75;153;68;110.5;97.75;246.5;28.5;28.5"
ShowProducts.Clear 'CLEAR THE LIST BOX BEFORE REBUILDING THE LIST
With Product_baseline
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row 'FIND THE LAST ROW NUMBER
For i = 1 To LastRow 'ADD ITEMS TO THE LISTBOX
 ShowProducts.AddItem .Cells(i, 1).Value 'ADD NEW ROW
 ShowProducts.List(i - 1, 1) = .Cells(i, 2).Value 'ADD TO ADJACENT COLUMNS ON SAME ROW
 ShowProducts.List(i - 1, 2) = .Cells(i, 3).Value
 ShowProducts.List(i - 1, 3) = .Cells(i, 4).Value
 ShowProducts.List(i - 1, 4) = .Cells(i, 5).Value
 ShowProducts.List(i - 1, 5) = .Cells(i, 6).Value
 ShowProducts.List(i - 1, 6) = .Cells(i, 7).Value
 ShowProducts.List(i - 1, 7) = .Cells(i, 8).Value
 ShowProducts.List(i - 1, 8) = .Cells(i, 9).Value
 ShowProducts.List(i - 1, 9) = .Cells(i, 10).Value
Next i
End With
 
How would you propose passing the array back to the lisbox, Norie?

I tried a simple test using a filtered range with
VBA Code:
With UserForm1.ListBox1
    .ColumnCount = 7
    .Clear
    .List = ConsolidateAreasToArray(Selection.SpecialCells(12))
End With
UserForm1.Show
Which populates the listbox with the correct data, but it has been incorrectly transposed.
Attempting to transpose the range being passed to the function results in RTE 424
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Ignore post #11, I was trying to transpose the range before passing it to the function, I just realised that transposing the entire function works fine.
 
Upvote 0
Oops, forgot to transpose the array at the end of the function.:eek:

Mind you, not essential when using it to populate a listbox, since you can use Column instead of List.
 
Upvote 0
@jasonb75 @Norie

Is there an alternative way to do this without using the copy method i.e. value = value

I'm finding that the copy method every 1 in 25ish attempts hangs for 10 seconds, appears to be a known issue with copying data?

VBA Code:
Product_Baseline.Range("A1:I" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("A1") ' copy filtered data to temp sheet
 
Upvote 0
The code I posted doesn't use Copy.:eek:
 
Upvote 0
I was unable to use your approach Norie as my original range of data went from A1 to BB & lastrow (682) , but I only wanted 12 of those columns in ListBox, so I only copied those columns over to the Temp location

VBA Code:
ProductDetails.Range("A1:A" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("A1") ' copy filtered data to temp sheet
                    ProductDetails.Range("G1:G" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("B1") ' copy filtered data to temp sheet
                    ProductDetails.Range("P1:P" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("C1") ' copy filtered data to temp sheet
                    ProductDetails.Range("R1:R" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("D1") ' copy filtered data to temp sheet
                    ProductDetails.Range("S1:S" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("E1") ' copy filtered data to temp sheet
                    ProductDetails.Range("W1:W" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("F1") ' copy filtered data to temp sheet
                    ProductDetails.Range("X1:X" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("G1") ' copy filtered data to temp sheet
                    ProductDetails.Range("Y1:Y" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("H1") ' copy filtered data to temp sheet
                    ProductDetails.Range("AD1:AD" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("I1") ' copy filtered data to temp sheet
                    ProductDetails.Range("AG1:AG" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("J1") ' copy filtered data to temp sheet
                    ProductDetails.Range("AN1:AN" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("K1") ' copy filtered data to temp sheet
                    ProductDetails.Range("AZ1:AZ" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("L1") ' copy filtered data to temp sheet
                    ProductDetails.Range("BB1:BB" & LastRow).SpecialCells(xlVisible).Copy Sheets("Temp").Range("M1") ' copy filtered data to temp sheet
 
Upvote 0
Remaining code used in current approach

Code:
lasttmp = Sheets("Temp").Cells(Rows.Count, 1).End(xlUp).Row ' find last row of temp sheet

Code:
        With ShowProducts
                        .ColumnCount = 13
                        .ColumnWidths = "25;40;85;155;75;115;100;250;30;30;30;250;150"
                        .RowSource = vbNullString
                        .Clear
                        .RowSource = "Temp!" & Sheets("Temp").Range("A1:M" & lasttmp).Address ' set data source as temp sheet
                    End With
 
Upvote 0
Norie's method would be more efficient if the code can be modified to do what you need.
I'll leave the changes to Norie, if I try to do it then it would be like engraving glass with a sledgehammer, a lack of precision and finesse ?
 
Upvote 0

Forum statistics

Threads
1,214,938
Messages
6,122,346
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top