我的代码包含两个组件:
我的代码,用于将表从另一个Excel文件(位于同一文件夹中)复制到活动工作簿中。 (此代码有效。):
Sub Copy_YTD()
Dim wsCopy as Worksheet
Dim wsDest as Worksheet
Dim CopyLastRow As Long
Workbooks.Open "C:\Users\....."
Set wsCopy = Workbooks("QA_Allocations_YTD.xlsm").Worksheets("YTD")
Set wsDest = Workbooks("YTD_Report_Generator.xlsm").Worksheets("YTD")
CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
wsCopy.Range("A1:AZ" & CopyLastRow).Copy _
wsDest.Range("A4")
Workbooks("QA_Allocations_YTD.xlsm").Close
End Sub
下面是在现有工作表中创建数据透视表的代码。
当我尝试从工作表运行它时,会生成
“运行时错误'1004':应用程序定义或对象定义的错误”
AND
错误代码“ 400”
Sub Create_Pivot_Table()
Dim destws As Worksheet
Dim wb As Workbook
Dim pc As PivotCache
Dim pt As PivotTable
Set destws = Worksheets("Funding Type Breakdown")
Set wb = ThisWorkbook
'below Range("QA_Allocations_YTD") is ALWAYS the table name which is copied,
' I also tried Range(Sheets("YTD").Range("QA_Allocations_YTD")) but to no success
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Range("QA_Allocations_YTD"))
Set pt = destws.PivotTables.Add(PivotCache:=pc, TableDestination:=Range("E10"), TableName:="P1")
End Sub
我尝试了不同的方法;这是最简单的,我遇到了同样的错误消息。
答案 0 :(得分:0)
尝试一下:
rb.MovePosition(Vector3.MoveTowards(...))