如何解决如何在Excel中动态更新数据透视表?
Option Explicit
Sub DPTR()
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Dim startcell As Range,lastRow As Long,lastCol As Long
Dim ws,wsPivot As Worksheet
Dim pvtName As String,rngPivot As Range,strNewData As String
Set ws = ThisWorkbook.Worksheets(4)
Set wsPivot = Sheet8
pvtName = "CFN"
Set startcell = ws.Range("A1")
lastRow = ws.Cells(ws.Rows.Count,startcell.Column).End(xlUp).Row
lastCol = ws.Cells(startcell.Row + 1,ws.Columns.Count).End(xlToLeft).Column
Set rngPivot = ws.Range(startcell,ws.Cells(lastRow,lastCol))
strNewData = ws.Name & "!" & rngPivot.Address(ReferenceStyle:=xlR1C1)
'If WorksheetFunction.CountBlank(rngPivot.Rows(1)) > 0 Then
'MsgBox "One of the source data columns contains a blank heading." & vbNewLine _ & "Please add a
heading and run the macro again",vbCritical,_
'"Source data Column heading missing"
'On Error GoTo Reset
'End If
wsPivot.Pivottables(pvtName).ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase,SourceData:=strNewData)
wsPivot.Pivottables(pvtName).RefreshTable
Reset:
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
上面的代码引发了运行时错误5。我试图尽我所能修复它,但是我做不到。有人可以帮我吗?
我正在尝试动态更新数据透视表的范围。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。