View Full Version : VBA to stack pivot table data

04-23-2012, 10:49 AM
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.

Bob Phillips
04-23-2012, 11:12 AM
Cross-posted at MrExcel http://www.mrexcel.com/forum/showthread.php?t=630621

04-23-2012, 02:15 PM
Thanks - just an update. Right now I have the following code that stacks the data -

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

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

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.