数据透视表macrosExcel

在这里输入图像说明 我目前有一个Excel电子表格有一些头像

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

但是在我的数据透视表中,我只对Pers.No. 和“小时”等等。 不幸的是,我从中获得原始数据的程序多次给予人员编号,并显示他们每天所做的工作。 所以,会有多个员工12345条目,但我想在数据透视表中显示一次,并将分配给此12345所有小时数加起来。 这当然可以手动完成,但不是太可怕,我需要做一个macros,让别人可以使用它。

 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 

理想情况下,我希望它创build一个新的表1与数据透视表在其中

– – – – – – 编辑 – – – – – – – – – – – – – –

在这里输入图像说明

鉴于这与一堆更多的条目,我想创build一个数据透视表,看起来像

 Employee ID Hours 12345 90 123423 100 

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

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

  Set objTable = Sheet1.PivotTableWizard 

  Set objTable = Sheets("May20").PivotTableWizard 

希望它能让你进一步进行。透视表向导应该在新表中创build数据透视表。

编辑

我得到一个错误的对象所需的行“Set objField = objTable.PivotFields(”Hours“)”

如果您的代码中的字段名称和实际工作表不匹配,则可能导致此错误。

下面的代码稍微修改基于你的代码工作正常。 我还附加了示例数据,数据透视表和报告的快照。 如果在运行程序时遇到错误,则在再次运行程序之前,通过按ALT + Q从VBE始终需要注意从VBE退出debugging模式,并且还要删除包含数据透视表的工作表作为预防措施。

 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 

样本数据
数据透视表
报告

编辑

我觉得如果你想过滤一个特定的人,它应该在页面字段。 在我logging的macros之后,如快照中所示,为选定的人员提供输出。

 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