数据透视表宏Excel

时间:2016-07-12 14:03:37

标签: excel vba excel-vba macros

enter image description here我目前有一个excel电子表格,其中包含一些标题,如

Pers.No.  Employee name   Hours   etc etc
12345       bobby          5.5
12346       jones          6
12345       bobby          7.5

然而,在我的数据透视表中,我只对Pers.No感兴趣。和小时一样可以这样。不幸的是,我从中获取原始数据的程序会多次给出人数,并显示他们每天所做的工作。因此,员工12345会有多个条目,但我想在数据透视表中显示一次,并将分配给此12345的所有小时数相加。这当然可以手动完成而不是太可怕,但我需要制作一个宏,以便其他人可以使用它。

Person.No.     Hours
12345          40

我的尝试

Sub Macro()


    Dim objTable As PivotTable, objField As PivotField

    ' Select the sheet and first cell of the table that contains the data.
    ActiveWorkbook.Sheets("May20").Select
    Range("A1").Select

    ' Create the PivotTable object based on the Employee data on Sheet1.
    Set objTable = Sheet1.PivotTableWizard

    ' Specify row and column fields.
    Set objField = objTable.PivotFields("Pers.No.")
    objField.Orientation = xlRowField


    ' Specify a data field with its summary
    ' function and format.
    Set objField = objTable.PivotFields("Hours")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    objField.NumberFormat = "$ #,##0"



    ' Preview the new PivotTable report.
    ActiveSheet.PrintPreview

    ' Prompt the user whether to delete the PivotTable.
    Application.DisplayAlerts = False
    If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
        ActiveSheet.Delete
    End If
    Application.DisplayAlerts = True



End Sub

但是我不确定我是否以正确的方式执行此操作,因为我在行上遇到方法'pivottablewizard' of object '_worksheet' failed的错误

Set objTable = Sheet1.PivotTableWizard

理想情况下,我希望它创建一个带有数据透视表的新工作表

------------ EDIT ----------------------------

enter image description here

考虑到这一点,我想创建一些更多的条目 一个看起来像

的数据透视表
Employee ID   Hours

12345          90  
123423         100

现在,excel文件为具有相同ID的员工提供了多个条目,因此需要在数据透视表中汇总所有这些小时

1 个答案:

答案 0 :(得分:2)

请更改以下行以正确指向数据源。

 Set objTable = Sheet1.PivotTableWizard   

 Set objTable = Sheets("May20").PivotTableWizard

希望它能让你继续前进.Pivot Table Wizard应该在新表中创建数据透视表。

编辑

  

我在线上需要一个错误对象"设置objField =   objTable.PivotFields("时间&#34)"

如果代码和物理表中的字段名称不匹配,则可能导致此错误。

根据您的代码稍微修改以下代码工作正常。我还附加了样本数据,数据透视表和报告的快照。如果在运行程序时遇到错误,在再次运行程序之前按下ALT + Q,并且还要删除包含数据透视表的工作表,需要一个小心,从VBE调出模式。

Sub Macro()


    Dim objTable As PivotTable, objField As PivotField

    ' Select the sheet and first cell of the table that contains the data.
    ActiveWorkbook.Sheets("May20").Select
    Range("A1").Select

    ' Create the PivotTable object based on the Employee data on Sheet1.
    Set objTable = Sheets("May20").PivotTableWizard

    ' Specify row and column fields.
    Set objField = objTable.PivotFields("Pers.No.")
    objField.Orientation = xlRowField


    ' Specify a data field with its summary
    ' function and format.
    Set objField = objTable.PivotFields("Hours")
    objField.Orientation = xlDataField
    objField.Function = xlSum
    'objField.NumberFormat = "$ #,##0"



    ' Preview the new PivotTable report.
    ActiveSheet.PrintPreview

    ' Prompt the user whether to delete the PivotTable.
    Application.DisplayAlerts = False
    If MsgBox("Delete the PivotTable?", vbYesNo) = vbYes Then
        ActiveSheet.Delete
    End If
    Application.DisplayAlerts = True



End Sub    

Sample data
Pivot Table
Report

修改

我觉得如果你想为特定的人过滤,它应该在页面字段中。以下由我录制的宏为快照中显示的所选人员提供输出。

Sub Macro2()
'
' Macro2 Macro
'

'
    Range("A1:E7").Select
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$E$7"), , xlYes).Name = _
        "Table1"
    Range("Table1[#All]").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Table1", Version:=6).CreatePivotTable TableDestination:="Sheet6!R3C1", _
        TableName:="PivotTable7", DefaultVersion:=6
    Sheets("Sheet6").Select
    Cells(3, 1).Select
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range("Sheet6!$A$3:$C$20")
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Pers.No.")
        .Orientation = xlPageField
        .Position = 1
    End With
    ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
        PivotTable.PivotFields("Hours"), "Sum of Hours", xlSum
    ActiveSheet.PivotTables("PivotTable7").PivotFields("Pers.No.").ClearAllFilters
    ActiveSheet.PivotTables("PivotTable7").PivotFields("Pers.No.").CurrentPage = _
        "12345"
    With ActiveSheet.PivotTables("PivotTable7").PivotFields("Employee name   H")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable7").PivotFields("CD1")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable7").PivotFields("CD2")
        .Orientation = xlRowField
        .Position = 3
    End With
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.PivotLayout.PivotTable.PivotFields("Pers.No.").ClearAllFilters
    ActiveSheet.PivotTables(-1).PivotFields("Pers.No.").CurrentPage = "12345"
    Range("B1").Select
    ActiveSheet.PivotTables("PivotTable7").ShowPages PageField:="Pers.No."
End Sub    

snapshot1
snapshot2
snapshot3