无论文件是否打开,我都想对名为“SearchData.xlsx”的文件应用过滤器。
我尝试了以下代码,但它给出了异常。
Sub ApplyFilterInDataFile()
IsOpen = False
For Each wb In Workbooks
If LCase(wb.Name) = "searchdata.xlsx" Then
IsOpen = True
End If
Next
If IsOpen Then
Workbooks("SearchData").ActiveSheet.UsedRange.AutoFilter Field:=42, Criteria1:=Range("SearchName")
Else
Set wb = Workbooks.Open(ThisWorkbook.Path & "\SearchData.xlsx")
Workbooks("SearchData").Activate
Workbooks("SearchData").ActiveSheet.UsedRange.AutoFilter Field:=42, Criteria1:=Range("SearchName")
wb.Close SaveChanges:=True
Set wb = Nothing
End If
End Sub
我需要专家帮助。