I am trying to create a UserForm.
I am following TheDataLabs video tutorial, but I am trying to adjust for 21 columns and thousands of rows of data.
I am stuck at the 32 minute mark. https://youtu.be/BdEMj4NNXAE?t=1921
The specific line:
shData.Range("A1:U" & iDataRow).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
Sub SearchData()
Application.ScreenUpdating = False
Dim shData As Worksheet ' Data sheet
Dim shSearchData As Worksheet 'SearchData sheet
Dim iColumn As Integer 'To hold the selected column number in database sheet
Dim iDataRow As Long 'To store the last non-blank row number available in Data sheet
Dim iSearchRow As Long 'To hold the last non-blank row number availble in Search Data sheet
Dim sColumn As String 'To store the column selection
Dim sValue As String 'To store the search text value
Set shData = ThisWorkbook.Sheets("Data")
Set shSearchData = ThisWorkbook.Sheets("SearchData")
iDataRow = ThisWorkbook.Sheets("Data").Range("A" & Application.Rows.count).End(xlUp).Row
sColumn = frmForm.cmbSearchColumn.value
sValue = frmForm.txtSearch.value
iColumn = Application.WorksheetFunction.Match(sColumn, shData.Range("A1:U1"), 0)
'Remove filter fom data worksheet
If shData.FilterMode = True Then
shData.AutoFilterMode = False
End If
'apply filter on Data worksheet
If frmForm.cmbSearchColumn.value = "Case code" Then
shData.Range("A1:U" & iDataRow).AutoFilter Field:=iColumn, Criteria1:=sValue
Else
shData.Range("A1:U" & iDataRow).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
End If
If Application.WorksheetFunction.Subtotal(3, shData.Range("C:C")) >= 2 Then
'Code to remove the previous data from SearchData worksheet
shSearchData.Cells.Clear
shData.AutoFilter.Range.Copy.shSearchData.Range ("A1")
Application.CutCopyMode = False
iSearchRow = shSearchData.Range("A" & Application.Rows.count).End(xlUp).Row
frmForm.lstDatabase.ColumnCount = 21
frmForm.lstDatabase.ColumnWidths = "30,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70"
If iSearchRow >= 1 Then
frmForm.lstDatabase.RowSource = "SearchData!A2:U" & iSearchRow
End If
Else
MsgBox "No record found."
End If
shData.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub
Solved it, thanks CLR for the help, it transpires that my range was in fact a table - i did not realise that needed to be referenced differently