gpt4 book ai didi

VBA 将数据透视表的源数据更新到行尾

转载 作者:行者123 更新时间:2023-12-02 07:55:17 25 4
gpt4 key购买 nike

我试图弄清楚当数据更改时如何使用 VBA 将数据透视表源数据更新到行尾。我当前的代码如下:

Dim shBrandPivot As Worksheet
Dim shCurrentWeek As Worksheet
Dim shPriorWeek As Worksheet
Dim shPivot As Worksheet
Dim lr As Long


Set shBrandPivot = ActiveWorkbook.Sheets("Brand Pivot")
Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week")
Set shPriorWeek = ActiveWorkbook.Sheets("Prior Week")
Set shPivot = ActiveWorkbook.Sheets("Pivot")
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row

With ActiveWorkbook.Sheets("Pivot").Activate

ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:="CurrentWeek!A3:X & lr"

End With

我收到的错误是运行时错误 1004:无法打开数据透视表源文件:E:\offline\KXM2103\Data\CurrentWeek

最佳答案

要仅在 VBA 中完成此操作,您可以尝试此操作。

Dim shBrandPivot As Worksheet
Dim shCurrentWeek As Worksheet
Dim shPriorWeek As Worksheet
Dim shPivot As Worksheet
Dim lr As Long
dim rng as range

Set shBrandPivot = ActiveWorkbook.Sheets("Brand Pivot")
Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week")
Set shPriorWeek = ActiveWorkbook.Sheets("Prior Week")
Set shPivot = ActiveWorkbook.Sheets("Pivot")
lr = shCurrentWeek.Range("A" & Rows.Count).End(xlUp).Row
set rng = shcurrentweek.range("A3:X" & lr)

With shPivot.PivotTables(1).PivotCache
.SourceData = rng.Address(True, True, xlR1C1, True)
.Refresh
End With

关于VBA 将数据透视表的源数据更新到行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13258001/

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