Consulting

Results 1 to 4 of 4

Thread: VBA to move pivot table field name to rows

  1. #1
    VBAX Newbie
    Joined
    Jan 2015
    Posts
    3
    Location

    VBA to move pivot table field name to rows

    I have posted this question on another forum without luck, so I thought I'd try here. I hope this is not against any rules, if so my apologies in advance.

    I have a bit of code that will move a field name in a pivot table to Rows. When I record the macro this field name is hard coded ( see example below).

    What I'd like to do is have the hard code value replaced with a value in a cell, so I can have data validation in this cell and change it rather than going into the code each time.

    In the 2nd example below I've tried something which I thought might work, but sadly it doesn't.


    Snipet of existing code

    With ActiveSheet.PivotTables("ReasonPivotTable").PivotFields("Reason name")
            .Orientation = xlRowField
            .Position = 1
        End With
    Snippet of code I'm trying to replace it with
    Dim PtableRowI As Variant
    
    
    Set PtableRowI = Sheets("DataRequestPt").Range("E3")
    
    
    With ActiveSheet.PivotTables("ReasonPivotTable").PivotFields.(PtableRowI)
                .Orientation = xlRowField
                .Position = 1
    End With

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Not tested but give this a shot

    Dim PtableRowI As String
    
    PtableRowI = Sheets("DataRequestPt").Range("E3").Value
    
    With ActiveSheet.PivotTables("ReasonPivotTable").PivotFields.(PtableRowI)
                .Orientation = xlRowField
                .Position = 1
    End With
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Jan 2015
    Posts
    3
    Location
    Thanks Paul, it's Sunday here at the moment and I will try this out tomorrow and let you know how it goes.

    Cheers
    Haydn

  4. #4
    VBAX Newbie
    Joined
    Jan 2015
    Posts
    3
    Location
    Thanks work your solution worked a treat

    Cheers
    Haydn

Tags for this Thread

Posting Permissions

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