+ Reply to Thread
Results 1 to 2 of 2

Anyone have a Macro that Automates Drag fields in a Pivot table

  1. #1
    Registered User
    Join Date
    10-02-2013
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question Anyone have a Macro that Automates Drag fields in a Pivot table

    Hi All,

    Would anyone be able to help a poor sap such as myself?
    I have data on Sheet1 and pivot table on Sheet2.
    I would like a Macro to automatically drag the fields (in this case the Columns) from Sheet1 into the fields/box.
    Basically Cell A1 from Sheet1 to the Row labels field box, then and Cells B1, C1, D1... etc in the Values field box.

    BUT, the columns in Sheet1 will constantly increase and change.

    is this possible?

    Regards,

  2. #2
    Registered User
    Join Date
    10-02-2013
    Location
    Toronto, Ontario
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Anyone have a Macro that Automates Drag fields in a Pivot table

    Found this on the internet!

    Sub test()


    Dim PCache As PivotCache
    Dim PT As PivotTable
    Dim wksSource As Worksheet
    Dim wksDest As Worksheet
    Dim i As Long

    Set wksSource = Worksheets("Sheet1") 'change the source worksheet name accordingly
    Set wksDest = Worksheets("Sheet2") 'change the destination worksheet name accordingly

    Set PCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=wksSource.Range("A1").CurrentRegion)

    Set PT = wksDest.PivotTables.Add( _
    PivotCache:=PCache, _
    TableDestination:=wksDest.Range("A2"), _
    TableName:="MyPivotTable")

    With PT
    For i = 1 To .PivotFields.Count
    If i > 1 Then
    .PivotFields(i).Orientation = xlDataField
    Else
    .PivotFields(i).Orientation = xlRowField
    End If
    Next i
    .DisplayFieldCaptions = False 'optional
    .TableStyle2 = "PivotStyleMedium2" 'optional
    End With

    wksDest.Select

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2014, 01:16 PM
  2. Pivot table losing column fields when run from macro
    By medeng in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2012, 11:15 AM
  3. run time error 1004 when using macro to change pivot Table fields.
    By Craigs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2011, 06:19 AM
  4. Excel 2007 : Macro to add Pivot table Data fields
    By vikrantm26 in forum Excel General
    Replies: 1
    Last Post: 07-09-2010, 08:49 AM
  5. How to Disable automatic fields in pivot table like total and count on fields
    By anushka in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-24-2009, 07:53 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1