0

I am trying to filter a table based on a cell in another workbook, the reference for the filter would be in workbook "ReportWB" and the "Project Summary Detail" sheet, cell "I1" (which I believe is covered by the "Region" string? The user would type the number into the "Region" and then it should filter the table in the "DetailData" Workbook and then copy that filtered range. At the moment I am getting an error on the autofilter part I believe due to no having the criteria cited correctly. The Error is Run-Time Error '1004': AutoFilter method of Range class failed in this code line 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```
1
  • 2
    I want to say the AutoFilter Criteria1 is expecting an array of values, but you are using an object. I think you need to add .Value right after ...Range("I1")
    – gns100
    Commented Feb 29 at 0:11

0

You must log in to answer this question.

Browse other questions tagged .