根據另一個工作簿中的儲存格引用過濾表格,然後複製它

根據另一個工作簿中的儲存格引用過濾表格,然後複製它

我正在嘗試根據另一個工作簿中的單元格過濾表格,過濾器的參考將在工作簿“ReportWB”和“項目摘要詳細信息”工作表中,單元格“I1”(我相信它由“區域”覆蓋) “字串?使用者將數字輸入“區域”,然後它應該過濾“DetailData”工作簿中的表,然後複製過濾後的範圍。目前我在自動過濾部分收到錯誤,我認為是由於沒有正確引用標準。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```

相關內容