Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

create macro to create pivot table and change format

Posted on 2011-09-29
2
Medium Priority
?
179 Views
Last Modified: 2012-05-12
hello,
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
testA.xlsx
testB.xlsx
0
Comment
Question by:davetough
2 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 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 = _
        "MyTable"
    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:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/A_3172-How-to-Automatically-Update-Your-PivotTables-and-PivotCharts.html
0
 

Author Closing Comment

by:davetough
ID: 36818872
thanks works great
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

971 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