0

I have a problem with Excel Pivot that I've been struggling to solve for hours now. I have the code below which I got from doing a Record Macro. It is supposed to create a Pivot table and add "Record Type" as first column then add another column for the total records for each record type but it deletes the 1st column. I am trying to do this http://youtu.be/UseY0lEPu80 but when I use VBA I get this problem http://youtu.be/eOxXX336gP0

S1LastRow = 569
S1LastColumn = 17
Sheet2.UsedRange.Delete
Application.Goto Sheet2.Range("A1"), True

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= "Main!R1C1:R" & S1LastRow & "C" & S1LastColumn, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:="Graph!R1C1", TableName:="Summary", DefaultVersion :=xlPivotTableVersion12

With ActiveSheet.PivotTables("Summary").PivotFields("Record Type")
    .Orientation = xlRowField
    .Position = 1
End With

ActiveSheet.PivotTables("Summary").AddDataField ActiveSheet.PivotTables("Summary").PivotFields("Record Type"), "Count of Record Type", xlCount

With ActiveSheet.PivotTables("Summary")
    .ColumnGrand = False
    .RowGrand = False
End With
2
  • Add the field as a data field first, then as a row field and you won't have that problem. Commented Dec 2, 2014 at 16:50
  • Sorry Roy but could you explain more or maybe an example rearrangement of my code above? I thought I already did what you suggested though. The problem is the .AddDataField removes the previous column :s Commented Dec 2, 2014 at 18:29

1 Answer 1

1

Do it Like this:

Dim PC                    As Excel.PivotCache
Dim PT                    As Excel.PivotTable

S1LastRow = 569
S1LastColumn = 17
Sheet2.UsedRange.Delete
Application.Goto Sheet2.Range("A1"), True

Set PC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
                                    SourceData:="Main!R1C1:R" & S1LastRow & "C" & S1LastColumn, _
                                    Version:=xlPivotTableVersion12)
Set PT = PC.CreatePivotTable(TableDestination:="Graph!R1C1", TableName:="Summary", _
                             DefaultVersion:=xlPivotTableVersion12)

With PT

    ' add the data field first if you want to use the same field as a row/column field too
    .AddDataField .PivotFields("Record Type"), "Count of Record Type", xlCount

    With .PivotFields("Record Type")
        .Orientation = xlRowField
        .Position = 1
    End With

    .ColumnGrand = False
    .RowGrand = False
End With
Sign up to request clarification or add additional context in comments.

2 Comments

Ah I see, so the problem there was that I was using the same column as both Pivotfield and Datafield which rarely happens by the way. Is it safe to say that we should practice adding the Datafields first before the Pivotfield to avoid problems? I tried your code and it works but I went back to my code above and modified it so it does the .AddDataField first before the .PivotFields and it now works too.
Yes. When you add it as a Row/Column field the name doesn't change, so when you add it as a datafield, it moves the existing field. When you add it as a datafield, the name has to be different, so the subsequent code works to add a new copy of the field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.