运行时错误“13”:在macros中键入不匹配

我录制了一个macros来创build一个枢轴图。 数据源被定义为一个variables,因为数据不断变化。 macros运行良好的行数(至20号)。 但随着数据增加超过20号。 macros的显示

运行时错误13:types不匹配。

代码如下。 突出显示的部分显示错误

Dim s As Worksheet, t As String Dim i As Long, K As Long K = Sheets.Count For i = K To 1 Step -1 t = Sheets(i).Name If t = "COMPARISON" Then Application.DisplayAlerts = False Sheets(i).Delete Application.DisplayAlerts = True End If Next i Sheets.Add(After:=Sheets(Sheets.Count)).Name = "COMPARISON" Sheets("FRONT PAGE").Select Nobid = Cells(11, 4) Sheets("ARRANGED BIDS").Select Finalrow = Cells(Rows.Count, 1).End(xlUp).Row **ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15). _ CreatePivotTable TableDestination:="COMPARISON!R1C1", TableName:="PivotTable1" _ , DefaultVersion:=xlPivotTableVersion15** Sheets("COMPARISON").Select Cells(1, 1).Select ActiveWorkbook.ShowPivotTableFieldList = True ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select ActiveChart.SetSourceData Source:=Range("COMPARISON!$A$1:$C$18") ActiveSheet.Shapes("Chart 1").IncrementLeft 192 ActiveSheet.Shapes("Chart 1").IncrementTop 15 With ActiveChart.PivotLayout.PivotTable.PivotFields("Item No.") .Orientation = xlRowField .Position = 1 End With With ActiveChart.PivotLayout.PivotTable.PivotFields("Item Description") .Orientation = xlRowField .Position = 2 End With With ActiveChart.PivotLayout.PivotTable.PivotFields("Quantity") .Orientation = xlRowField .Position = 3 End With ActiveWindow.SmallScroll Down:=-6 With ActiveChart.PivotLayout.PivotTable.PivotFields("Item No.") .Orientation = xlRowField .Position = 3 End With With ActiveChart.PivotLayout.PivotTable.PivotFields("Quantity") .Orientation = xlRowField .Position = 1 End With 

请有人帮我解决这个错误。 谢谢

尝试下面的代码,它应该采取你的错误,和所有的PivotTable设置。 解释是在代码注释中。

注意 :我删除了所有不合格的RangeCells ,也删除了不必要的Select ,这会降低代码的速度。

 Option Explicit Sub copy_to_report() Dim PvtTbl As PivotTable Dim PvtCache As PivotCache Dim PvtRng As Range Dim ws As Worksheet 'Dim t As String Dim i As Long Dim FinalRow As Long Dim Nobid As Long Application.DisplayAlerts = False ' loop through all sheets, if one of them is name "COMPARISON" delete it For Each ws In ThisWorkbook.Worksheets If ws.Name Like "COMPARISON" Then ws.Delete Next ws Application.DisplayAlerts = True Set ws = Sheets.Add(After:=Sheets(Sheets.Count)) ws.Name = "COMPARISON" ' ***** THIS is the only variable I'm not sure what it's used for ***** Nobid = Sheets("FRONT PAGE").Cells(11, 4) With Sheets("ARRANGED BIDS") ' find last row in column "A" in "ARRANGED BIDS" sheet FinalRow = .Cells(.Rows.Count, 1).End(xlUp).Row ' set the Range for the Pivot Cache Set PvtRng = .Range(.Cells(2, 1), .Cells(Finalrow, (7 + 2 * Nobid))) End With ' Option 1: Set Pivot Cache Set PvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, PvtRng) ' Option 2: Set Pivot Cache 'Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PvtRng.Address(True, True, xlA1, True)) ' create a new Pivot Table in "COMPARISON" sheet Set PvtTbl = ws.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=ws.Range("A1"), TableName:="PivotTable1") End Sub 

要么你试试这个…

 ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15). _ CreatePivotTable TableDestination:="COMPARISON!A1", TableName:="PivotTable1" 

或者像这样尝试…

 Dim pc As PivotCache Dim pt As PivotTable Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheets("ARRANGED BIDS").Range(Cells(2, 1), Cells(Finalrow, (7 + 2 * Nobid))), Version:=xlPivotTableVersion15) Set pt = pc.CreatePivotTable(TableDestination:="COMPARISON!A1", TableName:="PivotTable1") 

如果你想创build一个数据透视表,这里是我使用的。 删除所有的“xl”。 从代码中,我是在新的Excel中创build它们并保存它们。

  Dim wb As Workbook Dim ws As Worksheet Dim rng1 As Range Set wb = xl.ActiveWorkbook Set ws = wb.Sheets("Data") Set rng1 = ws.Cells.Find("*", ws.[a1], xlFormulas, , , xlPrevious) xl.Worksheets.Add xl.ActiveSheet.Name = "Pivot" xl.Worksheets("Pivot").Activate xl.ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ "Data!R1C1:R" & CStr(rng1.Row) & "C" & CStr(rng1.Column), Version:=xlPivotTableVersion15).CreatePivotTable _ TableDestination:="Pivot!R3C1", TableName:="PivotTable3", DefaultVersion _ :=xlPivotTableVersion15 xl.Sheets("Pivot").Select xl.ActiveWorkbook.ShowPivotTableFieldList = True With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status") .Orientation = xlRowField .Position = 1 End With With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("CompCode") .Orientation = xlRowField .Position = 2 End With With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("Aging from last action in PD") .Orientation = xlColumnField .Position = 1 End With With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("PD#") .Orientation = xlDataField .Position = 1 .Function = xlCount .NumberFormat = "0" .Name = "Count of PD#" End With xl.ActiveSheet.PivotTables("PivotTable3").HasAutoFormat = False Dim j As Variant xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status"). _ EnableMultiplePageItems = True With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status") For j = 1 To .PivotItems.Count DoEvents .PivotItems(.PivotItems(j).Name).Visible = False With xl.ActiveSheet.PivotTables("PivotTable3").PivotFields("IC Status") On Error Resume Next .PivotItems("In progress AP Timisoara").Visible = True End With Next j End With