我是Excel VBA的新手,现在我在处理数据透视表时遇到了困难。数据透视表用于计算分数和分数发生次数。
以下是必须使用数据透视表的一些条件:
以下是使用数据透视表来实现我的要求的我的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
我非常沮丧地获得数据透视表中的分数范围。一些专家建议我使用第二行(添加一个新列),但我尝试过它仍然没有用。由于我的堆栈溢出声誉不超过10分,我无法上传我的快照以使其更清晰,希望我的描述可以帮助您理解这个问题。提前谢谢。
答案 0 :(得分:0)
Excel提供的分组功能适用于大多数情况。对于此类示例,您可以指定要分组的范围。要在VBA中执行此操作就像获取对字段中某个单元格的引用并在其上调用Group
一样简单。
这是代码
Sub GroupPivot()
Range("D3").Group Start:=60, End:=90, By:=10
End Sub
这是一张包含普通菜单和分组调用结果的图像。