VBA Array Subscript out of Range again?

Dsewardj

Board Regular
Joined
Dec 30, 2008
Messages
155
Alright, I was gaining my sanity but now I feel it slipping yet again. I have populated an array in VBA with a range from a worksheet. Now I am trying to loop through the array and populate a listbox but it is throwing a Subscript Out of Range error. Below is my code again:

Code:
Private Sub listBoxMasterTemplate_Click()
Dim x As Integer
Dim lColumn As Double
Dim wksName As String
Dim HeaderArray As Variant
'Determine which Item was selected in ListBox
For x = 0 To listBoxMasterTemplate.ListCount - 1
    If listBoxMasterTemplate.Selected(x) = True Then
        wksName = listBoxMasterTemplate.List(x)
    End If
Next x
'If no item was selected
If wksName = "" Then
    MsgBox "Please select a worksheet."
    Exit Sub
End If
'Determine how many columns are in header and populate array
With wbsMasterTemplate.Sheets(wksName)
    lColumn = .Range("IV1").End(xlToLeft).Column
    HeaderArray = .Range(.Cells(1, 1), .Cells(1, lColumn)).Value
End With
'now populate the Header Listbox with the array
For x = 1 To UBound(HeaderArray)
    [B][COLOR=red]MsgBox HeaderArray(x)[/COLOR][/B]
    'frmOpenSheets.listBoxMasterTemplate.AddItem (HeaderArray(x))
Next
End Sub

The error is occurring in the red text. I don't get it?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Because you have array from a worksheet range, you have generated a 2 dimensional array so you need to give the second dimension you are extracting the value from (you have a horizontal array so write like this):

Rich (BB code):
MsgBox HeaderArray(1,x)
 
Upvote 0
headerArray is pulled from the Value property of a range, it is a two dimensional array

Dim headerArray(1 to RowCount, 1 to ColumnCount)

Even if RowCount or ColumnCount is 1, there are still two dimensions to headerArray

Two ways to approach this
1) use headerArray(1, x) in place of headerArray(x) throughout.
or
2) convert the 2D Range.Value to a 1D array
Code:
HeaderArray = .Range(.Cells(1, 1), .Cells(1, lColumn)).Value
HeaderArray = Application.Transpose(Application.Transpose(headerArray))
 
Upvote 0
Ah,

I did not realize that an array populated from a worksheet range was two dimensional. That definitely explains some things. Thanks again Richard.

Thanks Mikerickson, I like the idea of declaring the dimensions when declaring the variable. This is giving me some more ideas of what I can do now.

The more I learn the less I seem to know...
 
Upvote 0
I'm not suggesting that you run that Dim statement. I used it to illustrate what loading arrays from ranges did to the indices.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

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