我正在尝试从文件“ A:”运行宏,该文件包含特定文件夹中多个工作表上名为“ Filter_Criteria”的工作表。 在工作表“数据”中所选文件夹数据的每个“ B%”工作表中,应按文件A的工作表工作表“ Filter_Criteria”过滤,并将其发送回工作表“输出”文件“ B%”。 问题是文件夹中的所有文件都没有被过滤,我得到了任何“ B%”工作表的结果。
宏从开始到结束都没有问题。 使用以下命令在每个文件上运行宏代码时,内部宏代码可以正常工作:
Set Data_sh = ActiveWorkbook.Sheets("Data")
Set Output_sh = ActiveWorkbook.Sheets("Output")
但是我不明白当前的更改出了什么问题。
Sub RunOnAllFilesInFolder()
Dim folderName As String, eApp As Excel.Application, fileName As String
Dim wb As Workbook, ws As Worksheet, currWs As Worksheet, currWb As Workbook
Dim fDialog As Object: Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
Dim Filter_Criteria_Sh As Worksheet
Set currWb = ActiveWorkbook: Set currWs = ActiveSheet
Set Filter_Criteria_Sh = ThisWorkbook.Sheets("Filter_Criteria")
'Folder with Worksheets
fDialog.Title = "Select a folder"
fDialog.InitialFileName = currWb.Path
If fDialog.Show = -1 Then
folderName = fDialog.SelectedItems(1)
End If
'New Excel Process
Set eApp = New Excel.Application: eApp.Visible = False
fileName = Dir(folderName & "\*.*")
Do While fileName <> ""
'Update status bar to indicate progress
Application.StatusBar = "Processing " & folderName & "\" & fileName
Set wb = eApp.Workbooks.Open(folderName & "\" & fileName)
'Filter Data Macro
Dim Data_sh As Worksheet
Dim Output_sh As Worksheet
Set Data_sh = wb.Sheets("Data")
Set Output_sh = wb.Sheets("Output")
Output_sh.UsedRange.Clear
Data_sh.AutoFilterMode = False
Dim Emp_list() As String
Dim n As Integer
n = Application.WorksheetFunction.CountA(Filter_Criteria_Sh.Range("A:A")) - 2
ReDim Emp_list(n) As String
Dim i As Integer
For i = 0 To n
Emp_list(i) = Filter_Criteria_Sh.Range("A" & i + 2)
Next i
Data_sh.UsedRange.AutoFilter 2, Emp_list(), xlFilterValues
Data_sh.UsedRange.Copy Output_sh.Range("A1")
Data_sh.AutoFilterMode = False
MsgBox ("Data has been Copied")
wb.Close SaveChanges:=False
Debug.Print "Processed " & folderName & "\" & fileName
fileName = Dir()
Loop
eApp.Quit
Set eApp = Nothing
'Clear statusbar and notify of macro completion
Application.StatusBar = ""
MsgBox "Completed executing macro on all workbooks"
End Sub
我希望“过滤器数据宏将使用存储在工作表“ A”中的过滤器标准来过滤每个文件中的数据
答案 0 :(得分:0)
wb.Close SaveChanges:=False
您没有保存对工作簿所做的任何更改。