Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

excel - Add all fields into a Pivot Table as Max

I need to create a pivot table with all fields in the row section as MAX as the summarised value. I found a code that NEARLY does what I want here: https://www.pivot-table.com/2013/08/21/add-all-remaining-fields-to-pivot-table/

The only problem is this code doesn't set what kind of value is showed, so most of them came out as SUM or COUNT. Does anyone know what kind of changes to the code would make sure the values are MAX? Any help would be much appreciated!

Sub AddAllFieldsValues()
Dim pt As PivotTable
Dim iCol As Long
Dim iColEnd As Long

Set pt = ActiveSheet.PivotTables(1)

With pt    
   iCol = 1
   iColEnd = .PivotFields.Count

    For iCol = 1 To iColEnd
        With .PivotFields(iCol)
          If .Orientation = 0 Then
              .Orientation = xlDataField
          End If
        End With
    Next iCol
End With

End Sub
question from:https://stackoverflow.com/questions/65911036/add-all-fields-into-a-pivot-table-as-max

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can change it by using .Function in your case .Function = xlMax you will need to added after orientation. This method allows you to use count, sum, avg and other xl functions

        With .PivotFields(iCol)
          If .Orientation = 0 Then
             .Orientation = xlDataField
             .Function = xlMax
          End If

Now, beware that you are looping through all the pivot fields in your pivot table, therefore if you incorporate .Function = xlMax all will appear as max


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...