Excel 巨集:將工作表中的值貼到表中時發生錯誤

Excel 巨集:將工作表中的值貼到表中時發生錯誤

我正在嘗試從工作表“專案摘要資料”複製資料並將其貼上到工作表“專案成本報告摘要”上的表格中。我對巨集非常陌生,所以我不確定我在這裡做錯了什麼,但錯誤似乎是圍繞著這一部分tbl.Range("D7").Paste

我需要將資料貼到「專案成本報告摘要」上的儲存格「D7」中,從技術上講,該儲存格是表格中的第一行資料(無標題),也是表格的第四列。

我收到的錯誤是運行時錯誤“1004”:無法取得範圍類別的選擇屬性

Sub filter_copy_paste()

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


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")

'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.Range("D7").Paste
Application.CutCopyMode = False

'Show Data and Remove the Filter
With Data
.ShowAllData
.AutoFilterMode = False

End With

End Sub

相關內容