+ Reply to Thread
Results 1 to 4 of 4

Creating Pivot tables using dynamic columns

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Creating Pivot tables using dynamic columns

    Hi

    I am trying to cerate pivot table using a macro.

    The columns that I use for poviting are dynamically created, while running the same macro but prior to run this create pivot code, and also the titles change every week.

    Can you tell me how to use the dynamic titles in PivotFieds("field name")?

    Please see the sample code. it is failing at this point with 1004 error.

    With ActiveSheet.PivotTables("PivotTable1").PivotFields(filednamestring)
    .Orientation = xlRowField
    .Position = 1
    End With

    Thank you.
    Sakuntala

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,983

    Re: Creating Pivot tables using dynamic columns

    We'd need to see the rest of the code since we don't know how you built the cache or assigned the fieldnamestring (which may or may not contain a typo in your code)
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    07-22-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Creating Pivot tables using dynamic columns

    Hi,

    Please see the code below. For testing purpose I have hardcoded the column title in the variable Pvt2ColName. Actually this is the column inserted by another sub, whcich gets executed before this sub.

    For the variale Pvt2ColName = "Exceeds August Plan", the month name changes based on the date I run the macro.

    The error I am getting is 1004 and stopping at the line

    With ActiveSheet.PivotTables("PivotTable2").PivotFields(Pvt2ColName _
    )


    Sub FYPlanYTDPvt2()
    'FYPlanYTDPvt2(PvtRng)
    '
    ' Pvt2 Macro
    ' Macro recorded 8/5/2009 by schitta

    Dim PvtRng As Range
    Dim m, n As Integer


    Pvt2ColName = "Exceeds August Plan"

    Sheets("Report 1").Select
    Cells(1, 1).Select
    Set PvtRng = Range(Cells(1, 1), Cells(6795, 33))
    PvtRng.Select

    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
    PvtRng).CreatePivotTable TableDestination:="", TableName _
    :="PivotTable2", DefaultVersion:=xlPivotTableVersion10
    ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(5, 1)
    ActiveSheet.Cells(5, 1).Select
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("PivotTable2").PivotFields("Project CMR CC CTO")
    .Orientation = xlRowField
    .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable2").PivotFields(Pvt2ColName _
    )
    .Orientation = xlColumnField
    .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").PivotFields("Clarity Project ID"), "Count of Clarity Project ID" _
    , xlCount
    End Sub
    Last edited by Sakuntala; 08-06-2009 at 04:20 AM.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Creating Pivot tables using dynamic columns

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here


    Please edit both posts accordingly. Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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