Consulting

Results 1 to 3 of 3

Thread: VBA to stack pivot table data

  1. #1
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    2
    Location

    VBA to stack pivot table data

    Hi All -

    I work in a group where I get a lot of different data dumps that I need to import into a pivot table and trend out (monthly financial statement information).

    What i'll often get is monthly data in separate excel files, and I'll want to trend it out over months (for example, monthly trial balances). If i do this manually i'll have to stack each individual file into one file and then run a pivot. This takes a long time.

    In addition, sometimes the individual excel files have 3 column (GL, Description, Amount), and sometimes they have 2 columns (GL, amount), or sometimes they have 4 columns, etc. It just depends.

    I have a macro (attached) that takes the information in "Sheet1" which are my monthly trial balance and coverts it into pivot table "stacked" data in "Sheet2". You can see how it works by running "SetUpforPivot" in the macro.

    This makes my life so much easier (i just copy and paste each trial balance into sheet1 and I'm done).

    However, sometimes the individual excel files have 3 column (GL, Description, Amount), and sometimes they have 2 columns (GL, amount), or sometimes they have 4 columns, etc. It just depends. In order to correct this, I'll end up using concatenate to convert the data into two columns, bring it into the "stacked" pivot table, and then use text to column to split it back up into the original data format.

    I wanted to know if there was a way to change the code, such that, when i first run the macro, it ask me how many columns my data is (each month), and then adjust the macro so that if it's 3 columns it stacks 3 columns of data, and if it's 4, it stacks 4 columns of data. That way I don't have to use the concatenate function and/or text to column function anymore.
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,438
    Location
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    2
    Location
    Thanks - just an update. Right now I have the following code that stacks the data -

    [VBA]Sub PivotTableFormat()
    Dim wrk As Workbook 'Workbook object - Always good to work with object variables
    Dim sht As Worksheet 'Object for handling worksheets in loop
    Dim trg As Worksheet 'Pivot Worksheet
    Dim rng As Range 'Range object
    Dim colCount As Integer 'Column count in tables in the worksheets

    Set wrk = ActiveWorkbook 'Working in active workbook

    For Each sht In wrk.Worksheets
    If sht.Name = "Pivot" Then
    MsgBox "There is a worksheet called as 'Pivot'." & vbCrLf & _
    "Please remove or rename this worksheet since 'Pivot' would be" & _
    "the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
    Exit Sub
    End If
    Next sht

    'We don't want screen updating
    Application.ScreenUpdating = False

    'Add new worksheet as the last worksheet
    Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
    'Rename the new worksheet
    trg.Name = "Pivot"
    'Get column headers from the first worksheet
    'Column count first
    Set sht = wrk.Worksheets(1)
    colCount = sht.Cells(1, 255).End(xlToLeft).Column
    'Now retrieve headers, no copy&paste needed
    With trg.Cells(1, 1).Resize(1, colCount)
    .Value = sht.Cells(1, 1).Resize(1, colCount).Value
    'Set font as bold
    .Font.Bold = True
    End With

    'We can start loop
    For Each sht In wrk.Worksheets
    'If worksheet in loop is the last one, stop execution (it is Pivot worksheet)
    If sht.Index = wrk.Worksheets.Count Then
    Exit For
    End If
    'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(1048576, 1).End(xlUp).Resize(, colCount))
    'Put data into the Pivot worksheet
    trg.Cells(1048576, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
    'Fit the columns in Pivot worksheet
    trg.Columns.AutoFit

    'Screen updating should be activated
    Application.ScreenUpdating = True
    End Sub

    [/VBA]

    I got the VBA from the knowledge base on this website. I included the attached file that shows how the VBA returns the code, but I'm hoping to get it in a slightly different format which i've called "WhatIwant" in the attached file.
    Attached Files Attached Files

Posting Permissions

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