Results 1 to 4 of 4

creating dynamic number of rows in a listbox

Threaded View

anandvh creating dynamic number of... 06-15-2013, 07:30 AM
Tinbendr Re: creating dynamic number... 06-15-2013, 08:38 AM
nilem Re: creating dynamic number... 06-15-2013, 08:55 AM
anandvh Re: creating dynamic number... 06-15-2013, 09:38 AM
  1. #1
    Registered User
    Join Date
    11-15-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2007
    Posts
    97

    creating dynamic number of rows in a listbox

    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
    Last edited by anandvh; 06-15-2013 at 09:43 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1