Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel .PivotCache.SourceData导致应用程序或对象定义错误_Excel_Pivot Table_Vba - Fatal编程技术网

Excel .PivotCache.SourceData导致应用程序或对象定义错误

Excel .PivotCache.SourceData导致应用程序或对象定义错误,excel,pivot-table,vba,Excel,Pivot Table,Vba,我编写了以下代码来编辑两个透视表的范围。在代码正常运行之前,我会遇到一个运行时错误“1004”:在.PivotCache.SourceData=rng.AddressTrue,True,xlR1C1,True行上出现应用程序定义或对象定义的错误。我不知道错误的原因是什么,因为我从上面复制了代码,只更改了工作表名和透视表名,它们肯定都存在于我的文件中。有什么帮助吗 Dim RowCount As Integer Dim ColCount As Integer Dim rng As Range Di

我编写了以下代码来编辑两个透视表的范围。在代码正常运行之前,我会遇到一个运行时错误“1004”:在.PivotCache.SourceData=rng.AddressTrue,True,xlR1C1,True行上出现应用程序定义或对象定义的错误。我不知道错误的原因是什么,因为我从上面复制了代码,只更改了工作表名和透视表名,它们肯定都存在于我的文件中。有什么帮助吗

Dim RowCount As Integer
Dim ColCount As Integer
Dim rng As Range
Dim CurrentPeriod As String
Dim PivotList As Variant
Dim Piv As String
Dim PivotSht As String
Dim PivotNme As String

RowCount = WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
ColCount = WorksheetFunction.CountA(Sheets("Data").Range("1:1"))

Set rng = Sheets("Data").Range(Sheets("Data").Cells(1, 1), Sheets("Data").Cells(RowCount, ColCount))

CurrentPeriod = Sheets("Static").Range("CurrentPeriod")

With Sheets("Val Cat Current Returns (Adj)").PivotTables("CatCurrentPivot1")
    .PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
    .PivotFields("Period_id").CurrentPage = CurrentPeriod
    .PivotCache.Refresh
End With

**********************************
With Sheets("Val Cat Trend Returns (Adj)").PivotTables("CatTrendPivot1")
    .PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
    .PivotCache.Refresh
End With

经过数小时的调查,我无法找出是什么原因导致了我的文件中的错误。最后,我使用了.PivotCache.SourceData=rng.AddressTrue,True,xlR1C1,True的替代方法,即SheetsPivotSht.PivotTableWizard SourceType:=xlDatabase,SourceData:=rng。为了使用这种方法,必须在选定的透视表中选择一个单元格。要解决此问题,可以使用SheetsPivotSht.PivotTablesPivotNme.TableRange2.Cells1.Address查找数据透视表左上角的单元格。下面是我的完整代码,它引用了静态工作表,其中列出了我的所有透视表名称和工作表。另请参阅Siddharth Rout的评论,以了解查找数据范围的更好方法

Sub CurrentPeriodPivots()

Dim RowCount As Integer
Dim ColCount As Integer
Dim rng As Range
Dim CurrentPeriod As String
Dim PivotList As Variant
Dim Piv As String
Dim PivotSht As String
Dim PivotNme As String
Dim PivotLoc As String

RowCount = WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
ColCount = WorksheetFunction.CountA(Sheets("Data").Range("1:1"))

Set rng = Sheets("Data").Range(Sheets("Data").Cells(1, 1), Sheets("Data").Cells(RowCount, ColCount))

CurrentPeriod = Sheets("Static").Range("CurrentPeriod")

For i = 8 To 9
    PivotSht = Sheets("Static").Cells(i, 1).Value
    PivotNme = Sheets("Static").Cells(i, 2).Value
    PivotLoc = Sheets(PivotSht).PivotTables(PivotNme).TableRange2.Cells(1).Address
    Sheets(PivotSht).Select
    Range(PivotLoc).Select
    Sheets(PivotSht).PivotTableWizard SourceType:=xlDatabase, SourceData:=rng
    Sheets(PivotSht).PivotTables(PivotNme).PivotFields("Period_id").CurrentPage = CurrentPeriod
    Sheets(PivotSht).PivotTables(PivotNme).PivotCache.Refresh
Next i


For i = 10 To 16
    PivotSht = Sheets("Static").Cells(i, 1).Value
    PivotNme = Sheets("Static").Cells(i, 2).Value
    PivotLoc = Sheets(PivotSht).PivotTables(PivotNme).TableRange2.Cells(1).Address
    Sheets(PivotSht).Select
    Range(PivotLoc).Select
    Sheets(PivotSht).PivotTableWizard SourceType:=xlDatabase, SourceData:=rng
    Sheets(PivotSht).PivotTables(PivotNme).PivotCache.Refresh
Next i


End Sub

黑暗中的一枪。。试试这个。PivotCache.SourceData=SheetsData.Name&rng.AddressTrue,True,xlR1C1,True谢谢你的建议,但仍然得到相同的错误。我测试了代码,你的代码对我有效。虽然我不赞成你如何找到最后一行和最后一列。SheetsData.CellsRowCount,ColCount。如果a列中有一个空白单元格,那么您的行计数将进行掷骰。我建议使用查找最后一行和最后一列来构建范围