Loading array into list box

G

Guest

I'm trying to load an array into a list box. When I execute the following
macro, it tells me that I have a subscript out of range for the AddItem code.

Sub PopulateListBox()
Dim MyArray As Variant
Dim Ctr As Integer
MyArray = Sheet1Range("A1:A4").Value
For Ctr = LBound(MyArray) To UBound(MyArray)
UserForm1.ListBox1.AddItem MyArray(Ctr)
Next
UserForm1.Show
End Sub
 
R

Ron de Bruin

Use this event in the userform module

Private Sub UserForm_Initialize()
Me.ListBox1.List = Sheet1.Range("A1:A4").Value
End Sub
 
B

Bob Phillips

It's 2 dimensional

Dim MyArray As Variant
Dim Ctr As Integer
MyArray = Range("A1:A4").Value
For Ctr = LBound(MyArray) To UBound(MyArray)
UserForm1.ListBox1.AddItem MyArray(Ctr, 1)
Next
UserForm1.Show


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

You either use a multi-column listbox and load it as 2D, or loop through the
cells and load as in your example.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top