Problem populating listbox from range (Subscript out of range error)

jmbhoy

New Member
Joined
May 9, 2009
Messages
6
Hi,

On the change of a combobox that lists all worksheets within the current workbook, I am trying to dynamically populate a list box on the same form, from data contained in the 1st row of the chosen worksheet.

I am receiving a "subsript out of range" error on the .AddItem MyList(i) line. My code is below, any help in resolving this would be really appreciated.

Many Thanks :)

Example Worksheet = "Sheet 1"
A B C D E F
1
Col1 Col2 Col3 Col4 Col5 Col6

List Box Data should be

Col1
Col2
Col3
Col4
Col5
Col6

------------------------------------------------------------------

Private Sub ComboBox1_Change()
Dim WorksheetName As String
Dim rngSel As String
Dim lastcol As Long
Dim MyList As Variant, i As Long

WorksheetName = ComboBox1.Value

'Check for optional worksheetname else use activesheet
If WorksheetName = vbNullString Then
WorksheetName = ActiveSheet.Name
End If

With Worksheets(WorksheetName)

With Me.lbxColumns

.Clear
.ColumnCount = 1
.TextColumn = True

' Calls function to retrieve last populated column on spreadhseet
lastcol = xlLastCol(WorksheetName)

' Retrieves Excel data range and converts to address string
rngSel = (Range((Cells(1, 1)), (Cells(1, lastcol))).Address)

MyList = Worksheets(WorksheetName).Range(rngSel).Value ' get the values you want
MyList = Application.WorksheetFunction.Transpose(MyList) ' convert values to a vertical array

For i = 1 To UBound(MyList)
.AddItem MyList(i) '
<<<< SUBSCRIPT OUT OF RANGE ERROR
Next i


.ListIndex = 0

End With

End With

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
It's actually a 2D array, so you need to specify the second dimension:
Rich (BB code):
.AddItem MyList(i, 1)
 
Upvote 0

Forum statistics

Threads
1,215,942
Messages
6,127,807
Members
449,408
Latest member
Bharathi V

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