运行时错误' 13':宏中的类型不匹配

时间:2017-04-13 07:21:37

标签: excel vba excel-vba type-mismatch

我录制了一个宏来创建数据透视表。数据源被定义为变量,因为数据不断变化。宏运行良好的行数较少(直到20号)。但随着数据增加超过20没有。行,宏显示

  

运行时错误13:类型不匹配。

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

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

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

3 个答案:

答案 0 :(得分:2)

尝试下面的代码,它应该是您的错误,以及所有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

答案 1 :(得分:0)

要么你试试这个......

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")

答案 2 :(得分:0)

如果你想创建一个支点,这就是我使用的。删除所有&#34; xl。&#34;从代码中,我在新的Excel上创建它们并保存它们。

    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