[英]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 . 数据透视表用于计数分数和分数出现 。
Here are some conditions that must use the Pivot Table: 以下是必须使用数据透视表的一些条件:
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.