Consulting

Results 1 to 5 of 5

Thread: Create Pivot table using VBA

  1. #1
    VBAX Regular
    Joined
    Apr 2015
    Location
    Paris
    Posts
    13
    Location

    Create Pivot table using VBA

    Hi,

    I'm trying to create a Pivot table but I have no idea how. I looked online on how to do it but I don't understand the explanations.

    Can anyone please help me. I have 5 items that need to go in the table:

    1) Consultant (Left-most side of the table)
    2) Margin
    3) Interim
    4) X
    5) Y

    Items 2-5 are amounts that should appear from left to right in the columns after the Consultants. So Consultants is the first column, Margin is the second etc.

    Any help will be appreciated. Thank you

    Chris

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    the best way to start is recording a macro while you are manually creating the table.

    then post the recorded macro if you want something more...
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Also don't forget to capture in the macro recorder any options, such as number formats, sort order, table layout, etc.

    You could make a dry run to make sure that the PT is the way you want it, and then record one for real

    The recorder will capture the exact ranges that you have in your initial data ("A1:Z26"), but the challenge (not hard) is to make it more general

    We can help with that
    ---------------------------------------------------------------------------------------------------------------------

    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

  4. #4
    VBAX Regular
    Joined
    Apr 2015
    Location
    Paris
    Posts
    13
    Location
    Hi guys thanks. I did it and it worked.

    I just have a question. How do you end up remembering the code for making a pivot table? It seems very long and complex. I'm unable to fully understand and remember it for future use.

    Ofcourse I can always copy and paste the code I just made and make the necessary adjustments but I won't be able to remember off by heart.

    Thanks a lot for your patience guys.

    Chris

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    How do you end up remembering the code for making a pivot table?
    I use the macro recorder and then generalize the recorded code, or

    I use a wrapper function that I created for me that suits the way I prefer to work


    'creates PT from .CurrentRegion around 'PivotRange' and put it on a new 'PivotSheet A1' and names it 'PivotName'
    '   pass single value or array of PivotDataFields
    '       and/or PivotRowFields and/or PivotRowFields and/or PivotPageFields
    'create PT (Input range, output sheet name, PivotName,
    '   array(data), array (rows), array (cols), array (pages),
    '   quick format = all data = sum, grandtotals and subtotals, shading on total lines
    '
    'data, row, column, page items can be "LIST_NAME=NEW_NAME;NUMBERFORMAT"
    
    Function PT_Create(PivotRange As Range, PivotSheet As String, PivotName As String, PivotDataFields As Variant, _
            Optional PivotRowFields As Variant, Optional PivotColFields As Variant, Optional PivotPageFields As Variant, _
            Optional PivotQuickFormat As Boolean = True) As PivotTable
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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