run-time error '9' subscript out of range

Magic Polygon

New Member
Joined
Aug 20, 2023
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
When attempting to browse records, I'm told ""run-time error '9' subscript out of range, with the line

VBA Code:
ReDim Preserve SearchListBoxArray(1 To RowCount, 1 To 13)

highlighted in yellow on the debug screen, which is part of the below procedure that takes some information from the user to find and produce a list of records satisfying the given information.

VBA Code:
Private Sub DisplayRecords()

    Dim SearchTerm As String
    Dim SearchColumn As String
    Dim RecordRange As Range
    Dim FirstAddress As String
    Dim FirstCell As Range
    Dim RowCount As Integer
    Dim SearchListBoxArray() As Variant
    Dim SearchArrayColumn As Integer

    'some code
    
    ' Only search in the relevant table column i.e. if somone is searching Location
    ' only search in the Location column
    With Worksheets("Appointments").ListObjects("AppointmentsTable").ListColumns(SearchColumn).Range

        ' Find the first match
        Set RecordRange = .Find(SearchTerm, LookIn:=xlValues)

        '
        FirstAddress = RecordRange.Address
        RowCount = 0

        Do
            
            'Create a new row for ListBox
            RowCount = RowCount + 1
            ReDim Preserve SearchListBoxArray(1 To RowCount, 1 To 13) 'This highlights when clicking debug
                
            ' Set the first cell in the row of the matching value
            Set FirstCell = Range("B" & RecordRange.Row)
                
            ' Add matching record to List Box
            SearchResultsListBox.AddItem
                
            For SearchArrayColumn = 1 To 13
                    
                'Populate the array with data from the appointment worksheet
                SearchListBoxArray(RowCount, SearchArrayColumn) = FirstCell(1, SearchArrayColumn)
                
            Next SearchArrayColumn
                
            ' Look for next match
            Set RecordRange = .FindNext(RecordRange)

            ' When no further matches are found, exit the sub
            If RecordRange Is Nothing Then

                'Populate ListBox using the built array
                SearchResultsListBox.List = SearchListBoxArray

                Exit Sub

            End If

        ' Keep looking while unique matches are found
        Loop While RecordRange.Address <> FirstAddress

    End With
End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Initially rowcount is 1, so ReDim Preserve SearchListBoxArray(1 To RowCount, 1 To 13) would be...
ReDim Preserve SearchListBoxArray(1 To 1, 1 To 13)... seems like it should be OK (I'm not sure if XL likes the 1 to 1 part though) but only the 1st dimension of the array is preserved and the 2nd dimension is erased, so it's not going to work anyways. If you redim/dimension the array outside of the loops by determining the rowcount before the loops then it should work. HTH. Dave
 
Upvote 1
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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