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:
The error is occurring in the red text. I don't get it?
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?