Magic Polygon
New Member
- Joined
- Aug 20, 2023
- Messages
- 30
- Office Version
- 2019
- Platform
- Windows
When attempting to browse records, I'm told ""run-time error '9' subscript out of range, with the line
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:
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