Fill Listbox with Multiple Ranges

mucah!t

Well-known Member
Joined
Jun 27, 2009
Messages
593
Hi all,

Is it possible to fill a ActiveX Listbox with multiple dynamic ranges?
[ie; A4-C10, D4-F18, G4-I9]
 
If you want the List one complete column at a time , try this.
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Sep58
[COLOR="Navy"]Dim[/COLOR] Rng     [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Ray     [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] Ac      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rw      [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] A       [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("A4:C10,D4:F18,G4:I9")
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] A [COLOR="Navy"]In[/COLOR] Rng.Areas
    Ray = Application.Transpose(Application.Transpose(A.Value))
        [COLOR="Navy"]For[/COLOR] Ac = 1 To UBound(Ray, 2)
            [COLOR="Navy"]For[/COLOR] Rw = 1 To UBound(Ray, 1)
                [COLOR="Navy"]If[/COLOR] Ray(Rw, Ac) <> "" [COLOR="Navy"]Then[/COLOR]
                    ListBox1.AddItem Ray(Rw, Ac)
                [COLOR="Navy"]End[/COLOR] If
            [COLOR="Navy"]Next[/COLOR] Rw
        [COLOR="Navy"]Next[/COLOR] Ac
    [COLOR="Navy"]Next[/COLOR] A
    
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This awesome Mick, but not yet what I had in mind.
Perhaps below example clarifies it.

Code:
A1	B1	C1	D1	E1	F1
MIKE	ALFA	21	RICK	KILO	143
JOHN	BRAVO	54	MATTHEW	LIMA	12
LEX	CHARLIE	13	LEX	MIKE	36
PETER	DELTA	87			
					
					
LISTBOX					
MIKE	ALFA	21			
JOHN	BRAVO	54			
LEX	CHARLIE	13			
PETER	DELTA	87			
RICK	KILO	143			
MATTHEW	LIMA	12			
LEX	MIKE	36
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG12Sep50
[COLOR="Navy"]Dim[/COLOR] Rng     [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Ray     [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]Dim[/COLOR] A       [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] n       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] c       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] cl      [COLOR="Navy"]As[/COLOR] Range
c = 1
'[COLOR="Green"][B]Set Rng = Range("A4:C10,D4:F18,G4:I9")[/B][/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("A2:C5,D2:F4")
ReDim Ray(1 To Rng.Count, 1 To Rng.Columns.Count)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] A [COLOR="Navy"]In[/COLOR] Rng.Areas
   [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] cl [COLOR="Navy"]In[/COLOR] A
    [COLOR="Navy"]If[/COLOR] cl <> "" [COLOR="Navy"]Then[/COLOR]
    n = n + 1
    c = c + IIf(n = A.Columns.Count + 1, 1, 0)
    n = IIf(n = A.Columns.Count + 1, 1, n)
    Ray(c, n) = cl
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] cl
[COLOR="Navy"]Next[/COLOR] A
  
[COLOR="Navy"]With[/COLOR] ListBox1
  .ColumnCount = 3
  .ColumnWidths = "50,50,50"
  ListBox1.List = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hello Mick,

I had it working, but for some reason it does not work any longer.
It is stored in a module: [see below]
But running it gives "run-time error 424" "object required"


Code:
Sub test()
Dim Rng     As Range
Dim Ray     As Variant
Dim A       As Range
Dim n       As Integer
Dim c       As Long
Dim cl      As Range
c = 1

With Sheets("Dbase")
Set Rng = .Range("I2:K30,L2:N30")
End With

ReDim Ray(1 To Rng.Count, 1 To Rng.Columns.Count)
For Each A In Rng.Areas
   For Each cl In A
    If cl <> "" Then
    n = n + 1
    c = c + IIf(n = A.Columns.Count + 1, 1, 0)
    n = IIf(n = A.Columns.Count + 1, 1, n)
    Ray(c, n) = cl
End If
Next cl
Next A
  
With Listbox2
  .ColumnCount = 3
  .ColumnWidths = "120,40,40"
  Listbox2.List = Ray
End With
End Sub

Any idea?
 
Upvote 0
I don't know what the problem is, it works for me, does the code stop on any particular line.
Have you changed anything on the sheet or code.????
Instead of running it from a module , would it help if you ran from the sheet "Activate Event", then the Listbox would be full when you opened the sheet.
Code:
Private Sub Worksheet_Activate()
Dim Rng     As Range
Dim Ray     As Variant
Dim A       As Range
Dim n       As Integer
Dim c       As Long
Dim cl      As Range
c = 1
With Sheets("Dbase")
Set Rng = .Range("I2:K30,L2:N30")
End With
ReDim Ray(1 To Rng.Count, 1 To Rng.Columns.Count)
For Each A In Rng.Areas
   For Each cl In A
    If cl <> "" Then
    n = n + 1
    c = c + IIf(n = A.Columns.Count + 1, 1, 0)
    n = IIf(n = A.Columns.Count + 1, 1, n)
    Ray(c, n) = cl
End If
Next cl
Next A
  
With ActiveSheet.ListBox2
  .ColumnCount = 3
  .ColumnWidths = "120,40,40"
  ActiveSheet.ListBox2.List = Ray
End With
End Sub
Mick
 
Upvote 0
I used your suggested code.
Although it does not update upon opening it does not give an error when I run the code from VB?
So it now works when runned from VB.
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,946
Members
452,949
Latest member
beartooth91

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