Hi,
I am writing an application to load the number of members that match a criterion into a ListBox in a Userform. This is a multicolumn array
To start with I have defined the array with the maximum number of members that can be possibly populated into this list box. However, I find that if the actual number of members is less than the maximum number, blank rows are created in the ListBox. My code is as follows:
Private Sub UserForm_Initialize()
Dim MyArray As Variant
Dim tRow As Long
Dim iCRow As Long, AryRow As Long
tRow = Worksheets("PERS").Range("A" & Rows.Count).End(xlUp).Row
ReDim MyArray(1 To tRow - 2, 1 To 4) ' declare MyArray as a dynamic array
Dim iCurMn As Integer, sSubYr As String
iCurMn = Month(Now())
If iCurMn < 4 Then
sSubYr = Year(Now()) - 1 & " - " & Year(Now())
Else
sSubYr = Year(Now()) & " - " & Year(Now()) + 1
End If
' get the column number for the current financial year
' this is done by calling the find_finyr subroutine
Module1.Find_FinYr (sSubYr)
' start the population of the array
' start a for ... next loop going down each member
' for each member loops from column D (the first column for subscriptions)
' to the column number of the current financial year
' if there is a blank column, then the member has unpaid subscriptions
' the member is populated into the array, else member is skipped
Dim iColNo As Integer, lMemDues As Boolean
AryRow = 1
For iCRow = 3 To tRow
iColNo = 4
lMemDues = False
If ThisWorkbook.Worksheets("PERS").Range("D" & iCRow).Value = "Active" Then
Do Until iColNo = iFinYrCol + 1 ' loop from column D to the column for current financial year
If Worksheets("SUBS").Cells(iCRow, iColNo).Value = "" Then ' cell is blank
lMemDues = True ' flag for unpaid subscriptions
End If
iColNo = iColNo + 1 ' increment to next column
Loop
End If
' if flag for blank cell is set, then add the member to MyArray
' first column is the membership number, column C
' second column is the surname, column F
' third column is the first name, column G
' fourth column is the middle name, column H
If lMemDues = True Then
MyArray(AryRow, 1) = Worksheets("PERS").Cells(iCRow, 3).Value
MyArray(AryRow, 2) = Worksheets("PERS").Cells(iCRow, 6).Value
MyArray(AryRow, 3) = Worksheets("PERS").Cells(iCRow, 7).Value
MyArray(AryRow, 4) = Worksheets("PERS").Cells(iCRow, 8).Value
AryRow = AryRow + 1
End If
Next iCRow ' loop to the next member
' populate the combo box
Set lb = Me.MQlLstActn
With lb
.ColumnCount = 4
.List = MyArray
End With
End Sub
I tried to redim the array using the actual number of records populated which is the value for AryRow
ReDim Preserve MyArray(1 To (AryRow - 1), 1 To 4) ' declare MyArray as a dynamic array
This always gives me an error.
Hope some one will be able understand my code and help me with restricting the ListBox entries to the actual records populated
Anand
Bookmarks