AutoFilter method of Range class failed, Error 1004

sarrhian

New Member
Joined
Jan 10, 2022
Messages
11
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi there, I consider myself a novice at VBA and have managed to put together a UserForm that can search a worksheet and edit data. At some point along the line I seem to have broken the code as I can no longer search. The below is my code. The piece of code giving me this error is in red.
Code was written in Excel 2016 and also does not work in Excel 365.

Rich (BB code):
Sub SearchData()

    Application.ScreenUpdating = False
    Dim shDatabase As Worksheet 'Database Sheet
    Dim shSearchData As Worksheet 'Searchdata Sheet
    Dim iColumn As Variant
    Dim iDatabase As Long
    Dim iSearchRow As Long
    Dim sColumn As String
    Dim sValue As String
   
    Set shDatabase = ThisWorkbook.Sheets("Database")
    Set shSearchData = ThisWorkbook.Sheets("SearchData")
   
 
    iDatabase = ThisWorkbook.Sheets("Database").Range("A" & Application.Rows.Count).End(xlUp).Row
    sColumn = frmData.cmbSearch.Value
   
    sValue = frmData.txtSearch.Value
   
   
    iColumn = Application.Match(sColumn, shDatabase.Range("A1:M1"), 0)
   
    If shDatabase.FilterMode = True Then
        shDatabase.AutoFilterMode = False
    End If
   
    If frmData.cmbSearch.Value = "Asset No." Then
        shDatabase.Range("A1:M" & iDatabase).AutoFilter Field:=iColumn, Criteria1:=sValue
    Else
        shDatabase.Range("A1:M" & iDatabase).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
    End If
   
    If Application.WorksheetFunction.Subtotal(3, shDatabase.Range("C:C")) >= 2 Then
   
        'Code to remove previous search data
        shSearchData.Cells.Clear
       
        shDatabase.AutoFilter.Range.Copy shSearchData.Range("A1")
       
        Application.CutCopyMode = False
        iSearchRow = shSearchData.Range("A" & Application.Rows.Count).End(xlUp).Row
       
        frmData.lstDatabase.ColumnCount = 12
        frmData.lstDatabase.ColumnWidths = "0,60,75,40,60,45,55,0,70,70,70,70"
       
        If iSearchRow > 1 Then
            frmData.lstDatabase.RowSource = "SearchData!A2:T" & iSearchRow
        End If
    Else
        MsgBox "No record found."
    End If
   
    shDatabase.AutoFilterMode = False
    Application.ScreenUpdating = True
   
   
End Sub

Any help would be greatly appreciated!
 
Last edited by a moderator:
I then run into the issue of a Type Mismatch, error 13 on the below code.
That indicates the same problem as earlier. I don't think that the value held by the variable sColumn can be found (exactly) in the range A1:M1 on Sheet 'Database'

You seemed to be saying that sColumn holds the value "Asset No."
Is that correct?

If so, what cell in A1:M1 of 'Database' do you think also holds that exact value?
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Sorry, I'm having a bit of trouble understanding exactly what you mean.. I have attached my worksheet with dummy data. I really appreciate your help here!

dummy sheet.xlsm
ABCDEFGHIJKLM
1
2
3Details of AssetOwnerDetails of Inspection
4Asset No.Description of Asset (Inlucding model number)Serial NumberSupplierManufacturerPurchase DateL5Y/XYZIntervalPeriod InspectedLocated Yes/NoCondition
51BRE-A001Computer Desk123ABCDEFGIKEAIKEA15/02/2021XYZYearlyQ3-2020YesGood
62BRE-A002Computer ScreenGFEDCBA321IKEAIKEA15/02/2021L5YYearlyQ3-2020YesGood
73BRE-A003PrinterHIJKLMN123AustinBrother23/03/2019L5YYearlyQ3-2020YesGood
84BRE-A004Desktop Computer321NMLKJIHAustinDell20/05/2019XYZQuarterlyQ2-2020NoN/A
95BRE-A005Desktop ComputerQWERTY678AustinDell19/03/2020XYZQuarterlyQ4-2020YesGood
106BRE-A006Laptop 1LAPT123AustinAsus15/06/2021XYZQuarterlyQ2-2020YesGood
117BRE-A007Laptop 2LAPT234AustinAsus30/04/2020XYZQuarterlyQ2-2020YesGood
128BRE-A0083D PrinterPRiN323AustinN/A1/01/2022L5YQuarterlyQ3-2020YesGood
Database
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D5:D63Cell ValueduplicatestextYES
 
Upvote 0
iColumn = Application.Match(sColumn, shDatabase.Range("A1:M1"), 0)
Why are you looking in Row 1 when Asset No. appears in row 4 ?
 
Upvote 0
iColumn = Application.Match(sColumn, shDatabase.Range("A1:M1"), 0)
Why are you looking in Row 1 when Asset No. appears in row 4 ?
One of those moments of not noticing the little things I guess... x.x
The same error still shows up even when changed to A4:M4 though.
 
Upvote 0
The XL2BB you sent is definitely of the sheet "Database" right ?

Just for testing can you replace the line iColumn line with the below:-
and let us know what you get in the immediate window.

PS: You will also need to change yor other references to shDatabase.Range("A1 to be to A4


VBA Code:
    sColumn = "Asset No."               ' Add this line for testing
    iColumn = Application.Match(sColumn, shDatabase.Range("A4:M4"), 0)  ' Changed to row 4
    Debug.Print iColumn                 ' Add this line for testing
 
Upvote 0
I would be interested to see you insert this blue line of code where shown then run the code and see what the message box produces.

Rich (BB code):
iDatabase = ThisWorkbook.Sheets("Database").Range("A" & Application.Rows.Count).End(xlUp).Row
sColumn = frmData.cmbSearch.Value
MsgBox sColumn = shDatabase.Range("B4").Value
 
Upvote 0
I would be interested to see you insert this blue line of code where shown then run the code and see what the message box produces.

Rich (BB code):
iDatabase = ThisWorkbook.Sheets("Database").Range("A" & Application.Rows.Count).End(xlUp).Row
sColumn = frmData.cmbSearch.Value
MsgBox sColumn = shDatabase.Range("B4").Value
Sorry for the late reply, life things came up. The message box comes up with False.
 
Upvote 0
The XL2BB you sent is definitely of the sheet "Database" right ?

Just for testing can you replace the line iColumn line with the below:-
and let us know what you get in the immediate window.

PS: You will also need to change yor other references to shDatabase.Range("A1 to be to A4


VBA Code:
    sColumn = "Asset No."               ' Add this line for testing
    iColumn = Application.Match(sColumn, shDatabase.Range("A4:M4"), 0)  ' Changed to row 4
    Debug.Print iColumn                 ' Add this line for testing
Definitely Database.
When I use this code, it comes up with a message box saying True, then removed all of the data from the worksheet once it ran through the debug line.
Editing to clarify, I ran the code to search, it came up with the "True" message box and removed all of the data, so I entered some dummy data again, then when I stepped through the code in VBA it removed it all at the debug line.
 
Upvote 0
Based on the testing there seems to be a mismatch on what is being typed into the TextBox and the heading.
The most likely issue is that the full stop is not being typed into the text box.

It might be worth considering replacing your match line iColumn = Application.Match(sColumn, shDatabase.Range("A4:M4"), 0) with something line this:
VBA Code:
    Dim rngFound As Range
    With shDatabase.Range("A4:M4")
        Set rngFound = .Find(What:=sColumn, LookIn:=xlValues, MatchCase:=False, LookAt:=xlPart)
        If Not rngFound Is Nothing Then
            iColumn = rngFound.Column
        Else
            MsgBox "sColumn value: " & sColumn & " not found - exiting routine"
            Exit Sub
        End If
    End With
 
Upvote 0
Solution

Forum statistics

Threads
1,223,608
Messages
6,173,325
Members
452,510
Latest member
RCan29

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