我正在尝试根据另一个工作簿中的单元格过滤表格,过滤器的引用将在工作簿“ReportWB”和“项目摘要详细信息”工作表中的单元格“I1”(我认为该单元格被“Region”字符串覆盖?用户将在“Region”中输入数字,然后它应该过滤“DetailData”工作簿中的表格,然后复制该过滤范围。目前,我在自动筛选部分收到错误,我相信这是由于没有正确引用标准造成的。错误是运行时错误“1004”:Range 类的 AutoFilter 方法在此代码行中失败With DetailData Sheets("Project Detail").Range("A1").AutoFilter field = 2, Criteria1:=ReportWB.Sheets("Project Summary Data").Range("I1") DetailData.Sheets("Project Detail").Range("A1").CurrentRegion.Copy
Dim region As String
Dim Report As Worksheet
Dim Data As Worksheet
Dim count_col As Integer
Dim count_row As Integer
Dim tbl As ListObject
Dim DetailData As Workbook
Dim ReportWB As Workbook
Set Report = ThisWorkbook.Sheets("Project Cost Report Summary")
Set Data = ThisWorkbook.Sheets("Project Summary Data")
region = Data.Range("I1").Text
Set tbl = Report.ListObjects("Table2")
'Clean up Summary Sheet
Rows("9:3000").Select
Range("A3000").Activate
Selection.Delete Shift:=xlUp
Range("B7").Select
Selection.AutoFill Destination:=Range("Table2[Level]"), Type:=xlFillDefault
Range("Table2[Level]").Select
Columns("A:C").Select
Selection.EntireColumn.Hidden = True
'Clear Contents of Project cost Report Detail Tab
Sheets("Project Detail").Select
Cells.Select
Selection.ClearContents
'Clear Contents of Project cost Report Summary Tab
Sheets("Project Cost Report Summary").Select
Range("Table2[[Task Code]:[Budget $''s]]").Select
Selection.ClearContents
'## Open workbooks first:
Set DetailData = Workbooks.Open("C:\Users\sbossert\Documents\Macro Project Detail - All Data.xlsx")
Set ReportWB = Workbooks.Open("C:\Users\sbossert\Documents\Mid-Level Project Report - Template Filter data.xlsm")
'Now, copy what you want from Detail:
With DetailData
Sheets("Project Detail").Range("A1").AutoFilter field = 2, Criteria1:=ReportWB.Sheets("Project Summary Data").Range("I1")
DetailData.Sheets("Project Detail").Range("A1").CurrentRegion.Copy
'Now, paste to project detail worksheet:
ReportWB.Sheets("Project Detail").Range("A1").PasteSpecial
'Close x:
DetailData.Close
'determine the size of the range
Data.Activate
count_col = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlToRight)))
count_row = WorksheetFunction.CountA(Range("A1", Range("A1").End(xlDown)))
'filter data on Raw Data tab
Data.Range("A1").AutoFilter field:=1, Criteria1:=region
'Copy and Paste to Cost Report
Data.Range(Cells(2, 2), Cells(count_row, count_col)).SpecialCells(xlCellTypeVisible).Copy
tbl.DataBodyRange(1, 4).PasteSpecial
Application.CutCopyMode = False
'Show Data and Remove the Filter
With Data
.ShowAllData
.AutoFilterMode = False
'Project Detail have only project values
'Refresh All Data
End With
ReportWB.RefreshAll
End With
End Sub```