Excel VBA Run Time Error 1004 AutoFilter method of Range class failed - what is wrong?

Viewed 44

Hi Stack Overflow VBA community

I am completely new to VBA as of early last week, had only ever used recorded macros and only edited the recorded coded, so never went deep into developer mode. I had offered to create a UserForm for work, but i think I might have bitten off more than I can chew, as I keep getting this error but I am unsure why.

I am trying to follow TheDataLabs tutorial as through his 5 videos he gets it to the final point that I want to be at, but I am trying to adjust his code for having 21 columns and thousands of rows of data and seem to have got stuck somewhere

In terms of the video, the area of his explanation I am stuck at is the 32 minute mark or click here - https://youtu.be/BdEMj4NNXAE?t=1921

Please could someone assist me with the below code snippet? I have placed the specific line and the Sub

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

I have tried going back through the video multiple times, even downloading his file and pasting in certain snippets of relevant code etc.

I have looked through a few Stack Overflow questions on the same errors though can not spot any similar code so with my little experience I am unsure what is wrong

All help and guidance is much appreciated.

Thanks in advance.

0 Answers
Related