vba 当前文件夹所有excel 解除筛选
时间: 2023-08-13 21:03:22 浏览: 156
您可以使用以下 VBA 代码来解除当前文件夹中所有 Excel 文件的筛选:
```
Sub RemoveFilterFromAllExcelFilesInCurrentFolder()
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim objExcel As Object
Dim objWorkbook As Object
Dim objWorksheet As Object
'获取当前文件夹路径
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Application.ActiveWorkbook.Path)
'循环处理每个 Excel 文件
For Each objFile In objFolder.Files
If Right(objFile.Name, 5) = ".xlsx" Or Right(objFile.Name, 4) = ".xls" Then '仅处理 Excel 文件
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(objFile.Path)
'循环处理每个工作表
For Each objWorksheet In objWorkbook.Worksheets
'检查是否有筛选器
If objWorksheet.AutoFilterMode Then
objWorksheet.AutoFilterMode = False '解除筛选
End If
Next objWorksheet
objWorkbook.Close SaveChanges:=True
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
End If
Next objFile
Set objFolder = Nothing
Set objFSO = Nothing
End Sub
```
这段代码与前面的代码几乎相同,只是在获取文件夹路径的方式不同。它使用 `Application.ActiveWorkbook.Path` 来获取当前活动工作簿所在的文件夹路径。您可以将代码复制到 VBA 编辑器中 (按 Alt + F11 进入),然后按 F5 运行代码即可。
阅读全文