VBA数据透视表

ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=sh & "!R1C1:R" & rn & "C22", _
    Version:=4 _
    ).CreatePivotTable _
    TableDestination:="Sheet1!R1C1", _
    TableName:="数据透视表1", _
    DefaultVersion:=4
    'xlPivotTableVersion10=1(03)11=2()12=3()14=4(2010)~15=5(2013)6(2016)
    '必须在表激活情况下才能操作表中的数据透视表
With ActiveSheet.PivotTables("数据透视表1")
    '报表布局设置
    .RowAxisLayout xlTabularRow 'xlTabularRow表格,xlOutlineRow大纲,xlCompactRow压缩(默认)
    .RepeatAllLabels xlRepeatLabels 'xlRepeatLabels重复,xlDoNotRepeatLabels不重复(默认)
    '总计
    .ColumnGrand = False '.ColumnGrand列.RowGrand行'默认都启用True
    
    '行列筛选分类汇总域
    pf = Array("类型", "拣货员名称", "逻辑区号", "任务领取时间")
    For Each pfi In pf
    With .PivotFields(pfi)
        .Orientation = xlRowField
        .Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
    End With
    Next
    
    '.Orientation = xlRowField / xlColumnField / xlPageField
    '.Position = 1 '插入位置
    '.分类汇总 = Array (自动,求和,计数,平均值,最大值,最小值,乘积,数值计数,标准偏差,总体标准差,方差,总体方差)
    '.Subtotals = Array(Atuo,Sum,Count,Average,Max,Min,Product,Count Nums,StdDev,StdDevp,Var,Varp)
    '.SubtotalLocation xlAtTop '分类汇总位置xlAtTop顶部,xlAtBottom底部(默认)
    
    '值域与格式
    .AddDataField .PivotFields("拣货完成时间"), "最后拣货完成时间", xlMax
    .PivotFields("最后拣货完成时间").NumberFormat = "yyyy/m/d h:mm:ss"
    .AddDataField .PivotFields("实际拣货量"), "行", xlCount
    .AddDataField .PivotFields("实际拣货量"), "件", xlSum
    
    '全选透视表
    .PivotSelect "", xlDataAndLabel, True
    'PivotSelect 对象,模式,[允许宏]
    '模式:xlBlanks/xlButton/xlDataOnly/xlFirstRow/xlLabelOnly/xlOrigin/xlDataAndLabel(默认但必须)
End With

你可能感兴趣的:(VBA数据透视表)