input不匹配错误,同时添加pivotcache

这返回types不匹配错误,我不知道为什么。

我问昨天同样的任务,我发现有一些合并的单元格,因此我今天删除了合并单元格,但仍然不起作用给出相同的错误

该代码与其他工作表数据正常工作,但它返回该特定工作表的错误。

我试过这个:

Set wD = Sheets.Add ThisWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=wS.UsedRange).CreatePivotTable _ TableDestination:=wD.Range("A3") 

和这个

 Set wD = Sheets.Add ThisWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=wS.Range("A1").CurrentRegion).CreatePivotTable _ TableDestination:=wD.Name & "!R3C1" 

这是源数据表的样子

源数据表

试试下面的代码:

 Sub UpdatePivot() Dim wD As Worksheet Dim wS As Worksheet Dim PvtTbl As PivotTable Dim PvtCache As PivotCache Dim PvtRangeStr As String Set wS = Sheets("inventory master") Set wD = Sheets.Add PvtRangeStr = "'" & wS.Name & "'!" & wS.UsedRange.Address ' for debug Debug.Print PvtRangeStr ' set Pivot Cache using another reference to the range Set PvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PvtRangeStr, Version:=xlPivotTableVersion14) ' add this line in case the Pivot table doesn't exit >> first time running this Macro On Error Resume Next Set PvtTbl = wD.PivotTables("PivotTable1") On Error GoTo 0 If PvtTbl Is Nothing Then ' create a new Pivot Table in wD Sheet, start from Cell A3 Set PvtTbl = wD.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=wD.Range("A3"), TableName:="PivotTable1") Else ' just refresh the Pivot cache with the updated data PvtTbl.ChangePivotCache PvtCache PvtTbl.RefreshTable End If End Sub