別のワークブックのセルに基づいてテーブルをフィルター処理しようとしています。フィルターの参照は、ワークブック「ReportWB」と「Project Summary Detail」シートのセル「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```