The basic mission of this code is to use a list as the source of data for my a listbox control...with a catch. I only want the rows that have black cell in column 14 of the list.
To accomplish this, I attempted to assign an the cells to an array and assign the array using the list property.
I feel like I have read every refence document available and adhered to all the references, but I continually get this 'subscript out of range' error when 'redimming' the array in a preserved fashion after a for...next loop.
Before I use a temporary list to store my data construct, I really want to nail this dynamic array...but if it is too much work, then I'll have to settle for the easier option. Also, this is a learning process. Also, please forgive my sloppy indentations and everything else.
Option Explicit
'This code initializes the frmEntry form and sets the list box control
' to list the active escorts (escort records that have blank values
' in the 'End' field of the visitor log (VLog tabl on Visitor Log worksheet).
Private Sub UserForm_Initialize()
Dim wksVisitorLog As Worksheet
Dim wbkVMS As Workbook
Dim Last_Row As Long
Dim objVisitorEscortList As ListObject
Dim objListRow As ListRows
Dim objListCols As ListColumns
Dim listCounter As Single
Dim rowCounter As Single
Dim listArray()
Dim ri As Single
Dim ci As Single
Dim c As Single
Set wbkVMS = ThisWorkbook
Set wksVisitorLog = wbkVMS.Worksheets("Visitor Log")
Set objVisitorEscortList = wksVisitorLog.ListObjects("tblVisitorEscortLog")
Set objListRow = objVisitorEscortList.ListRows
Set objListCols = objVisitorEscortList.ListColumns
rowCounter = 0
ri = 0
ci = 0
c = 0
'Prepares the list box.
With frmEntry
.listboxActiveEscorts.Clear
.listboxActiveEscorts.ColumnCount = "15"
.listboxActiveEscorts.ColumnHeads = True
.listboxActiveEscorts.ColumnWidths = "80,100,100,0,0,100,100,0,0,50,0,0,80,80,80"
End With
ReDim listArray(ri, 14)
'This section adds Escort/Visitor records to list box
For listCounter = 1 To objListRow.Count 'Increments based on the total rows on "Visitor Log"
'Selects the row if the "End" field (14th column) is blank
If objVisitorEscortList.Range.Cells(listCounter + 1, 14) = "" Then
'Increments the row for the listbox array, and will only increment when the if condition is true
For ci = 0 To 14 'Starts inner loop index for the listbox control column
c = c + 1 'Increments the list range column of the "Visitor Log"
'This portion of the code assigns the two dimensional array index
listArray(ri, ci) = objVisitorEscortList.Range.Cells(listCounter + 1, c).Value
Next ci
End If
ReDim Preserve listArray(UBound(listArray, 1) + 1)
Next listCounter
'Assigns the entire array to list
listboxActiveEscorts.List = listArray
MsgBox "There are " & frmEntry.listboxActiveEscorts.ListCount & " total active escorts at this time", vbOKOnly
listCounter = 0
End Sub