在Excel VBA中创建一个数据透视表

时间:2018-05-11 17:32:12

标签: excel vba excel-vba pivot

我目前正在处理工作表,我需要对我的数据进行一些统计。我需要用VBA创建一个数据透视表。之后我需要用VBA制作一个图表。我有两个代码,它今天早些时候工作,但不知何故它现在不起作用。你能帮我解决这个问题吗?我将标记问题所在。谢谢

Sub AutoPivot1()

Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache

Dim PvtTblName As String
Dim pivotTableWs As Worksheet

PvtTblName = "pivotTableName"

' set the worksheet object where we will create the Pivot-Table
Set pivotTableWs = Sheets("Delayed Students")

' set the Pivot Cache (the Range is static)
Set PvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="Delayed Students!R1C1:R1532C7")

' create a new Pivot Table in the new created sheet
Set PvtTbl = pivotTableWs.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=pivotTableWs.Range("J1"), TableName:=PvtTblName)

' after we set the PvtTbl object, we can easily modifty all it's properties
With PvtTbl
    .ColumnGrand = True
    .HasAutoFormat = True
    .DisplayErrorString = False
    .DisplayNullString = True
    .EnableDrilldown = True
    .ErrorString = ""
    .MergeLabels = False
    .NullString = ""
    .PageFieldOrder = 2
    .PageFieldWrapCount = 0
    .PreserveFormatting = True
    .RowGrand = True
    .SaveData = True
    .PrintTitles = False
    .RepeatItemsOnEachPrintedPage = True
    .TotalsAnnotation = False
    .CompactRowIndent = 1
    .InGridDropZones = False
    .DisplayFieldCaptions = True
    .DisplayMemberPropertyTooltips = False
    .DisplayContextTooltips = True
    .ShowDrillIndicators = True
    .PrintDrillIndicators = False
    .AllowMultipleFilters = False
    .SortUsingCustomLists = True
    .FieldListSortAscending = False
    .ShowValuesRow = False
    .CalculatedMembersInFilters = False
    .RowAxisLayout xlCompactRow

    With .PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With

    .RepeatAllLabels xlRepeatLabels

    With .PivotFields("STUDYBOARD_ID")
        .Orientation = xlRowField
        .Position = 1
    End With

With .PivotFields("FACULTY_ID")
      .Orientation = xlDataField
      .Position = 1


End With
End With



End Sub

0 个答案:

没有答案