简体   繁体   English

使用VBA将新列添加到数据透视表中

[英]Add a new column into Pivot Table using VBA

I'm a newbie to Excel VBA and now I have a difficulty in dealing with the Pivot Table . 我是Excel VBA的新手,现在在处理数据透视表时遇到了困难。 The pivot table is used to count the scores and scores occurrence . 数据透视表用于计数分数和分数出现

  • I've finished the average function within the pivot table. 我已经完成数据透视表中的平均值功能。 (COMP) (COMP)
  • Now I would like to split the scores occurrence range. 现在,我想分割分数的出现范围。 For instance, how many persons scores are in the range of scores >=80, or >=70, or >=60, or even <60 (failed), 例如,有多少人的得分在得分范围> = 80或> = 70,或> = 60或什至<60(失败)中,
  • and after this, I also need to count the passed ratio 然后,我还需要计算合格率

Here are some conditions that must use the Pivot Table: 以下是必须使用数据透视表的一些条件:

  • One manager has many leaders, and one leader have many members, the members take the exams and count their scores. 一位经理有很多领导者,一位领导者有很多成员,成员参加考试并计算分数。
  • So the view of members' scores should bind with the manager and leader, to see each manager's data measure. 因此,成员评分的观点应与经理和领导者绑定,以查看每个经理的数据度量。

Below is my VBA code using Pivot Table to achieve my requirements: 以下是我使用数据透视表实现我的要求的VBA代码:

Private Sub CommandButton1_Click()
Dim objTable As PivotTable, objField As PivotField
Dim ws As Worksheet
Dim wsPivot As Worksheet

ActiveWorkbook.Sheets("Sheet1").Select
Range("A1").Select
Set objTable = Sheet1.PivotTableWizard

' Specify row and column fields
Set objField = objTable.PivotFields("Creator's Tower Lead")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Creator's Manager")
objField.Orientation = xlRowField
Set objField = objTable.PivotFields("Scores")
objField.Orientation = xlColumnField
'Set objField = objTable.PivotFields("Average Score")
'objField.Orientation = xlRowField
'Set objField = objTable.PivotFields("Score>=80")
'objField.Orientation = xlRowField
'Set objField = objTable.PivotFields("Score 70-79")
'objField.Orientation = xlRowField
'Set objField = objTable.PivotFields("Score 60-69")
'objField.Orientation = xlRowField
'Set objField = objTable.PivotFields("Score <60")
'objField.Orientation = xlRowField
'Set objField = objTable.PivotFields("Pass Rate (Score >60)")
'objField.Orientation = xlRowField


' Specify a data field with its summary
' function and format.
With objTable
    Set objField = objTable.PivotFields("Scores")
    objField.Orientation = xlDataField
    objField.Function = xlAverage
End With

With objTable
    Set objField = objTable.PivotFields("Scores")
    objField.Orientation = xlDataField
    objField.Function = xlCount
End With

'With objTable
'    Set objField = objTable.PivotFields("Scores")
'    objField.Orientation = xlDataField
'    Select Case objField
'        Case Scores > 80
'             objField.Function = xlCount
'        Case Scores > 70
'             objField.Function = xlCount
'        Case Scores > 60
'             objField.Function = xlCount
'        Case Else
'             objField.Function = xlCount
'    End Select
'End With End Sub

I really frustrated to get the scores range in pivot table. 我真的很沮丧要在数据透视表中获得分数范围。 And some experts suggest me use secondary row(add a new column), but I tried and it still didn't work. 并且一些专家建议我使用第二行(添加新列),但是我尝试了,但仍然行不通。 Due to my stack overflow reputation is not greater than 10 points, I cannot upload my snapshots to make it clearer, hope my description could help you understand this question. 由于我的堆栈溢出信誉不超过10分,因此我无法上传快照以使其更清晰,希望我的描述可以帮助您理解此问题。 Thanks in advance. 提前致谢。

Excel provides grouping functionality that works well for most cases. Excel提供的分组功能在大多数情况下效果很好。 For this type of example, you can specify the range to group by. 对于此类示例,您可以指定分组范围。 To do this in VBA is as easy as getting a reference to one of the cells in field and calling Group on it. 在VBA中执行此操作就像在字段中引用一个单元格并在其上调用Group容易。

Here is the code 这是代码

Sub GroupPivot()

    Range("D3").Group Start:=60, End:=90, By:=10

End Sub

And here is an image with the normal menu and the result of the grouping call. 这是带有普通菜单和分组调用结果的图像。

结果

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

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