使用VBA在动态数据上创建数据透视表时出现错误1004

时间:2015-02-17 06:36:10

标签: excel vba excel-vba pivot-table

使用VBA创建数据透视表时遇到以下问题。源数据是动态的。创建数据透视表时,我有时收到以下错误,它工作正常。

  

运行时错误'1004':
  数据透视表字段名称无效。若要创建数据透视表,您必须使用组织为带有标记列的列表的数据。如果要更改数据透视表字段的名称,则必须为该字段键入新名称。

'pivot table


'Adding new worksheet
Set ws = wbReport.Worksheets.add

'Set rangept = Sheets("Non_Complaince_CHOC_CA").UsedRange.Select
 'ls = wbReport.Sheets(ReportSheet).Cells(Rows.Count, 1).End(xlUp).Row
 ls = wbReport.Sheets(ReportSheet).Range("A" & Rows.Count).End(xlUp).Row

'Creating Pivot cache
Set pc = wbReport.PivotCaches.Create(xlDatabase, "" & ReportSheet & "!r1c1:r" & ls & "c12")


'Set pc = ActiveWorkbook.PivotCaches.Create(xlDatabase, "rangept")



'Creating Pivot table
Set pt = pc.CreatePivotTable(ws.Range("B3"))

'Setting Fields
With pt.PivotFields("Downtime Requirement")
.Orientation = xlRowField
.Position = 1
End With


'set value field

With pt.PivotFields("Downtime Requirement")
    .Orientation = xlDataField
    .Function = xlCount
    .Position = 1
End With
ws.Name = "Failing_PI_Summary"

ws.Range("B2:C2").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    ActiveCell.FormulaR1C1 = "Prod Domain Failing PI Summary for " & Client
    ws.Range("B2:C2").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    Selection.Font.Bold = True
    ws.Range("A1").Select
    Application.CutCopyMode = False
wbReport.Save
'end pivot table

0 个答案:

没有答案