gpt4 book ai didi

excel - 为什么更改数据透视表范围行上会出现无效过程或调用错误?

转载 作者:行者123 更新时间:2023-12-02 08:36:51 31 4
gpt4 key购买 nike

changepivottablecache 行有问题吗?

我尝试过此工作簿,但我能够执行该行来刷新表。

我在网上查看了几个修复程序,但没有一个按预期工作。

Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long

'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("Data")
Set Pivot_Sheet = ThisWorkbook.Worksheets("SheetTable")

'Enter in Pivot Table Name
PivotName = "PivotTable2"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)

'Change Pivot Table Data Source Range Address
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)

'Ensure Pivot Table is Refreshed
Pivot_Sheet.PivotTables(PivotName).RefreshTable

'Complete Message
Pivot_Sheet.Activate
MsgBox "Your Pivot Table is now updated."

End Sub

最佳答案

我能够使用以下代码成功更改现有的数据透视表。您可能需要适应您的情况。原始代码来自MSDN Forum

Sub ChangePTCache()
Dim PTRange As Range
Dim con_end As String
con_end = Range("'Sheet1'!A1").End(xlDown).Offset(0, 1).Address
Set PTRange = Range("'Sheet1'!A1:" & con_end)
Sheets(1).PivotTables(1).ChangePivotCache ActiveWorkbook.PivotCaches. _
Create(xlDatabase, PTRange)
End Sub

关于excel - 为什么更改数据透视表范围行上会出现无效过程或调用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52975662/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com