VBA代码只运行一次

时间:2016-10-01 08:03:48

标签: excel-vba caching pivot-table vba excel

我正在尝试运行以下代码,它运行正常并完成工作,但只执行一次。然后它开始抛出运行时错误1004:应用程序或对象定义的错误。 上下文:我在10个工作表中有大约15个数据透视表,每周更新一次。我每周更改源数据,我希望更新数据透视表。

10-01 17:35:06.065 10031-10031/project D/LOG: data source set
10-01 17:35:06.065 10031-10031/project D/LOG: preparing media player
10-01 17:35:06.067 10031-10066/project D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 6.0.1)
10-01 17:35:06.070 10031-10031/project D/MediaPlayer: setSubtitleAnchor in MediaPlayer
10-01 17:35:06.073 10031-10031/porject D/LOG: media player prepared
10-01 17:35:06.074 10031-10066/project D/MediaHTTPConnection: proxy null port 0
10-01 17:35:06.171 10031-10066/project W/MediaHTTPConnection: readAt 12648448 / 32768 => java.net.ProtocolException
10-01 17:43:43.352 21618-21618/project D/LOG: seeking to start
10-01 17:43:43.352 21618-21618/project W/MediaPlayer: Stream has no duration and is therefore not seekable.

我收到错误

Sub UpdateReports()

Dim wb As Workbook
Dim PT As PivotTable, PTCache As PivotCache, newPT As PivotTable

Set wb = Application.Workbooks("Open items VIM Analytics September 16th, 2016.xlsx")
Set PTCache = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wb.Worksheets("Vim open items 09.09.2016").Range("a:au"))
Set PT = wb.Worksheets("Per Location").PivotTables("PivotTable3")
Set newPT = wb.Worksheets("Per Location").PivotTables.Add(PivotCache:=PTCache, TableDestination:=wb.Worksheets("Per Location").Range("a200"))

PT.CacheIndex = newPT.CacheIndex

End Sub

1 个答案:

答案 0 :(得分:2)

如果这个就行了

Set newPT = wb.Worksheets("Per Location").PivotTables.Add(PivotCache:=PTCache, TableDestination:=wb.Worksheets("Per Location").Range("a200"))

它无法创建数据透视表,因为它已具有相同的名称。我建议在添加旧表之前尝试明确指定名称或删除。

您仍然可以尝试将旧表与新缓存链接。