VBA for Pivot Tables

Bacon117

New Member
Joined
Jul 2, 2010
Messages
11
Hope no one minds, I recreated this post with a better title, since I was getting no responses.

I need to quickly change which field is listed in the Values area of a pivot table. My current method is to drag the field out of the values area, drag the new field into the values area, right click it, and select average.


I would really like to write a <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> script to handle this for me based on a value in a cell (that has a drop down list populated from the header of my data). BUT, I cannot seem to find a way to determine WHICH field is currently in the values area before deleting it from the pivot table. All of the tutorials (and microsoft reference) show how to remove a field from a pivot chart, but only if you know which field is already there. I suppose I could remove everything, then add it all back, but I think that's a rabbit hold I'm not prepared for.

Any suggestions?

Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
This will remove all the value fields, then replace with one of your choice:


Excel 2010
ABCDEFGHIJKLMN
1NameGroupSeries1Series2
2QA6489
3WA6037Sum of Series2Column Labels
4EA5226Row LabelsABCDEFG
5RA977A98
6TB199D85
7YC8910E26
8UC3944F3
9IC2363G4
10OC7011H93
11PD8541I63
12AD7698J30
13SD8913K82
14DD6385L34
15FE583O11
16GF354P41
17HG9093Q89
18JG3330R77
19KG2782S13
20LG1334T99
21ZG2133U44
22W37
23Y10
24Z33
Test


Code:
Sub Macro1()
Dim PT As PivotTable, PTField As PivotField
Set PT = Sheets("Test").PivotTables("PivotTable1")
With PT
    .ManualUpdate = True
    For Each PTField In .DataFields
        PTField.Orientation = xlHidden
    Next PTField
    .ManualUpdate = False
    .AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Series1"), "Sum of Series1", xlSum
End With
Set PT = Nothing
End Sub


Excel 2010
GHIJKLMN
3Sum of Series1Column Labels
4Row LabelsABCDEFG
5A76
6D63
7E52
8F58
9G35
10H90
11I23
12J33
13K27
14L13
15O70
16P85
17Q64
18R9
19S89
20T1
21U39
22W60
23Y89
24Z21
Test


(Series2 becomes Series1)
 
Upvote 0
Hello, thank you for your reply. I appreciate it.

Unfortunately, this doesn't quite fix my problem. This will add the field to the pivot table, but does not specify which area the field goes to. The areas are "Report Filter", "Column Labels", "Row Labels", and "Values".

How do I specify that "Series1" is added to the "Values" area?
 
Last edited:
Upvote 0
Ok, I got it to work. It was actually causing an error, because I used the field name as the label name. So, thank you very much for that.

Is there anyway to specify that fields go into "Column Labels" or "Report Filters" instead of "Values"? Or do I have to manually setup the pivot Table first?

In other words, is there anyway to move Series1 to "Row Labels" or "Column Labels"?
 
Upvote 0
Something like this?

Code:
Sub Macro1()
Dim PT As PivotTable, PTField As PivotField
Set PT = Sheets("Test").PivotTables("PivotTable1")
With PT
    .ManualUpdate = True
    For Each PTField In .DataFields
        PTField.Orientation = xlHidden
    Next PTField
    .ManualUpdate = False
    .AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Series1"), "Sum of Series1", xlSum
        .PivotFields("Sum of Series1").Orientation = xlRowField
End With
Set PT = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,403
Messages
6,124,710
Members
449,182
Latest member
mrlanc20

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top