Results 1 to 3 of 3
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    add fields to pivottable via VBA (97, 2000, 2003)

    I have a snippet of VBA code that builds a pivot table from parameters that are passed into the subroutine. It works as expected:

    objXL.ActiveSheet.PivotTableWizard SourceType:=xlDatabase, _
    SourceData:="sheet1!LODMData", _
    TableDestination:="", _
    TableName:="LODMPivot"

    objXL.ActiveSheet.PivotTables("LODMPivot").AddFiel ds _
    RowFields:=psRowField, _
    ColumnFields:=psColField, _
    PageFields:=pvPageFields

    The parameter "psRowField" contains the value "Product" when this routine is called. I want to modify the pivot table by adding another row to the RowFields -- just doing this manually for now, with the expectation of putting the field name ('inventorymethod') into a new parameter. I added this line of code

    objXL.ActiveSheet.PivotTables("LODMPivot").AddFiel ds RowFields:="inventorymethod", addtotable:=True

    It creates the pivot table using ONLY the inventoryMethod, instead of adding it to the Product field that was already specified by the psRowField parameter. What's the correct syntax to add the new field into the Row area?

    FWIW, the VBA Intellisense changes the case of "rowfields" into "RowFields", but it does not make the corresponding change for "addtotable". Finally, this code is hosted in an Access 97 database that creates the XL spreadsheet.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: add fields to pivottable via VBA (97, 2000, 20

    Hi Jackson,
    I use the following code snippet (in Excel 2002, which seems to be the version you don't use!) to set up rowfields:
    <pre> With PT
    With .PivotFields("ASL")
    .Orientation = xlRowField
    .Position = 1
    End With
    With .PivotFields("ST")
    .Orientation = xlRowField
    .Position = 2
    End With
    </pre>

    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    Vancouver, Br. Columbia, Canada
    Posts
    632
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: add fields to pivottable via VBA (97, 2000, 2003)

    Rory
    Awesome. Worked like a charm. thanks.
    --------------------------------------------------
    Jack MacDonald
    Vancouver, Canada

Posting Permissions

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