Я получаю эту ошибку:
Ошибка времени выполнения '1004': сбой метода 'Range' объекта'_Worksheet '
После того, как я изменил этот код до:
iColumn = Application.Match(sColumn, shDatabase.Range("A1:U1"), 0)
Было:
iColumn = Application.WorksheetFunction.Match(sColumn, shDatabase.Range("A1:U1"), 0)
Теперь у меня есть эта ошибка в этом коде:
shDatabase.Range("A1:U" & iDatabaseRow).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
Sub SearchData()
Application.ScreenUpdating = False
Dim shDatabase As Worksheet ' Database sheet
Dim shSearchData As Worksheet 'SearchData sheet
Dim iColumn As Variant 'To hold the selected column number in Database sheet
Dim iDatabaseRow As Long 'To store the last non-blank row number available in Database sheet
Dim iSearchRow As Long 'To hold the last non-blank row number available in SearachData sheet
Dim sColumn As String 'To store the column selection
Dim sValue As String 'To hold the search text value
Set shDatabase = ThisWorkbook.Sheets("Database")
Set shSearchData = ThisWorkbook.Sheets("SearchData")
iDatabaseRow = ThisWorkbook.Sheets("Database").Range("A" & Application.Rows.Count).End(xlUp).Row
sColumn = frmForm.cmbSearchColumn.Value
sValue = frmForm.txtSearch.Value
iColumn = Application.Match(sColumn, shDatabase.Range("A1:U1"), 0)
'Remove filter from Database worksheet
If shDatabase.FilterMode = True Then
shDatabase.AutoFilterMode = False
End If
'Apply filter on Database worksheet
If frmForm.cmbSearchColumn.Value = "Project Number" Then
shDatabase.Range("A1:U" & iDatabaseRow).AutoFilter Field:=iColumn, Criteria1:=sValue
Else
shDatabase.Range("A1:U" & iDatabaseRow).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
End If
If frmForm.cmbSearchColumn.Value = "Customer" Then
shDatabase.Range("B1:U" & iDatabaseRow).AutoFilter Field:=iColumn, Criteria1:=sValue
Else
shDatabase.Range("B1:U" & iDatabaseRow).AutoFilter Field:=iColumn, Criteria1:="*" & sValue & "*"
End If
If Application.WorksheetFunction.Subtotal(3, shDatabase.Range("B:B")) >= 2 Then
'Code to remove the previous data from SearchData worksheet
shSearchData.Cells.Clear
shDatabase.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 = "25, 70, 50, 50, 70, 50, 30, 60, 60, 60, 60, 60, 60, 50, 50, 50, 50, 50, 90, 50, 90"
If iSearchRow > 1 Then
frmForm.lstDatabase.RowSource = "SearchData!A2:T" & iSearchRow
MsgBox "Records found."
End If
Else
MsgBox "No record found."
End If
shDatabase.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub