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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

734 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