简体   繁体   English

数据透视表宏Excel

[英]Pivot Table Macro Excel

在此处输入图片说明 I currently have an excel spreadsheet which has some headers like 我目前有一个Excel电子表格,其中包含一些标题,例如

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

However in my pivot table I am only interested in The Pers.No. 但是在我的数据透视表中,我只对Pers感兴趣。 and Hours to be avaliable like so. 和小时数这样。 Unfortunately the program where I get the original data from gives the persons number multiple times, and displays the work they do every day. 不幸的是,我从中获取原始数据的程序会多次给人员编号,并显示他们每天所做的工作。 So there would be multiple entries of employee 12345 but I want to show this once obviously in the pivot table, and sum all of the hours assigned to this 12345 . 因此,将有雇员12345多个条目,但我想在数据透视表中12345地显示一次,并对分配给此12345所有小时数求和。 This of course can be done manually not too horribly however, I need to make a macro so others can use it. 当然可以手动完成此操作,但是我需要制作一个宏,以便其他人可以使用它。

Person.No.     Hours
12345          40

My Attempt 我的尝试

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

However I am not sure if I am doing this the right way as I get an error of method 'pivottablewizard' of object '_worksheet' failed on the line 但是我不确定我是否以正确的方式执行此操作,因为我得到了'pivottablewizard' of object '_worksheet' failed的方法'pivottablewizard' of object '_worksheet' failed的错误,在线上'pivottablewizard' of object '_worksheet' failed

Set objTable = Sheet1.PivotTableWizard

Ideally I want it to create a new sheet 1 with the pivot table in it 理想情况下,我希望它使用其中的数据透视表创建一个新的工作表1

------------EDIT---------------------------- - - - - - - 编辑 - - - - - - - - - - - - - -

在此处输入图片说明

Given this with a bunch of more entries I would like to create A pivot table that looks like 鉴于此还有很多条目,我想创建一个看起来像这样的数据透视表

Employee ID   Hours

12345          90  
123423         100

Now the excel file has multiple entries for employees with the same ID so it needs to sum all those hours in the pivot table 现在,excel文件为具有相同ID的员工提供了多个条目,因此需要对数据透视表中的所有这些小时进行汇总

Please change the following line to correctly point to data source. 请更改以下行以正确指向数据源。

 Set objTable = Sheet1.PivotTableWizard   

To

 Set objTable = Sheets("May20").PivotTableWizard

Hope it will enable you to proceed further.Pivot Table Wizard should create Pivot Table in new sheet. 希望它可以使您继续前进。数据透视表向导应在新工作表中创建数据透视表。

EDIT 编辑

I get an error object required on line "Set objField = objTable.PivotFields("Hours")" 我在“ Set objField = objTable.PivotFields(“ Hours”)“行上得到一个错误对象

This error could be caused if the field names in your code and the physical sheet are not matching. 如果您的代码和物理工作表中的字段名称不匹配,则可能导致此错误。

Following code slightly modified based on your code is working fine. 根据您的代码稍作修改的以下代码可以正常工作。 I have also appended snapshots of sample data, pivot table and report. 我还附加了示例数据,数据透视表和报表的快照。 One care is needed always come out of Debug Mode from VBE by pressing ALT+Q before again running the program and also delete worksheet containing Pivot Table as a precautionary measure if you run into error while running the program. 在再次运行程序之前,请始终通过按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    

样本数据
数据透视表
报告

EDIT 编辑

I feel that if you want filtered for a particular person, it should be in page field. 我认为,如果您要为特定人员过滤,则应在页面字段中。 Following Macro recorded by me gives output for a selected person as shown in the snapshot. 由我录制的Macro后面提供了所选人员的输出,如快照中所示。

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    

快照1
快照2
快照3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM