create macro to create pivot table and change format

Posted on 2011-09-29
Last Modified: 2012-05-12
i have attached 2 worksheets.
testA and testB
first is it possible with a pivot table to change data in testA to look like testB?
second- can a macro be created to do it?
thank you
Question by:davetough
LVL 92

Accepted Solution

Patrick Matthews earned 500 total points
ID: 36818012
Yes, you can.  It is basically a PT with name and ID as row fields, date as column field, and sum of hours as data field.

The following macro seems to be working for me:

Sub MakePT()

    ActiveSheet.ListObjects.Add(xlSrcRange, Range("a1").CurrentRegion, , xlYes).Name = _
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="MyTable") _
        .CreatePivotTable TableDestination:="", TableName:=""
    With ActiveSheet.PivotTables(1)
        With .PivotFields("name")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("ID#")
            .Orientation = xlRowField
            .Position = 2
        End With
        With .PivotFields("date")
            .Orientation = xlColumnField
            .Position = 1
        End With
        .AddDataField .PivotFields("TotalHours"), "Sum of TotalHours", xlSum
        .RowAxisLayout xlTabularRow
        .PivotFields("name").Subtotals = Array(False, False, False, False, False, False, _
            False, False, False, False, False, False)
    End With
End Sub

Open in new window

I made the source range into a Table; for more on that please see:

Author Closing Comment

ID: 36818872
thanks works great

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question