我想一个文件夹中的应用过滤器到多个工作簿。在我的代码回路工作在一个给定的文件夹中打开所有.xlsm
文件,但我有申请一个循环过滤器的麻烦。我需要相同的过滤器适用于所有的工作簿。
我希望我真的很接近得到这个,它只是简单的东西,我在这里失踪。
首先,长宏打开给定文件夹的所有.xlsm
文件,但只有自动筛选活动工作簿,并不是所有的翻开的书本。第二个宏是我试图简化宏,但不只是成功的任何文件对话框打开,我可以选择的文件夹,但没有这种情况发生后。没有文件打开或过滤。
在宏观,我越来越
运行时错误438:对象不支持此属性或方法
在.autofilter field:=1....
Sub Main()
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
Application.ScreenUpdating = False
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xlsm")
Do While xFile <> ""
Workbooks.Open xStrPath & "\" & xFile
xFile = Dir
Loop
'Filter_Rows_By_RSSID
For Each xWB In Application.Workbooks
With Worksheets("Sheet1").Range("A1")
.AutoFilter field:=1, Criteria1:=Array("5649", "15899", "16583", "27314", "27471", "32551", "33111", "33124", "34404", "34607", "35157", "35331", "35546", "57203", "57450", "57803", "58119", "58413"), Operator:=xlFilterValues
End With
Next
End Sub
Sub BadLoopThroughFiles()
Dim xFd As FileDialog
Dim xFdItem As Variant
Dim xFileName As String
Set xFd = Application.FileDialog(msoFileDialogFolderPicker)
If xFd.Show = -1 Then
xFdItem = xFd.SelectedItems(1) & Application.PathSeparator
xFileName = Dir(xFdItem & "*.xls*")
Do While xFileName <> ""
With Workbooks.Open(xFdItem & xFileName)
.AutoFilter field:=1, Criteria1:=Array("5649", "15899", "16583", "27314", "27471", "32551", "33111", "33124", "34404", "34607", "35157", "35331", "35546", "57203", "57450", "57803", "58119", "58413"), Operator:=xlFilterValues
End With
xFileName = Dir
Loop
End If
End Sub
您With
块
With Workbooks.Open(xFdItem & xFileName)
.AutoFilter field:=1, Criteria1:=Array("5649", "15899", "16583", "27314", "27471", "32551", "33111", "33124", "34404", "34607", "35157", "35331", "35546", "57203", "57450", "57803", "58119", "58413"), Operator:=xlFilterValues
End With
会造成这个错误,因为你没有指定一个工作表,也不是你的自动筛选的范围。
你可以设置一个变量=你打开工作簿,并引用工作表和范围自动筛选。然后你就可以删除With
块,因为你只能用它了点。
Dim wb as Workbook
Set wb = Workbooks.Open(xFdItem & xFileName)
wb.Worksheets(1).Range("A1").AutoFilter field:=1, Criteria1:=Array("5649", "15899", "16583", "27314", "27471", "32551", "33111", "33124", "34404", "34607", "35157", "35331", "35546", "57203", "57450", "57803", "58119", "58413"), Operator:=xlFilterValues