Macro code to build a pivot table consolidated from multiple sheets

stu40

New Member
Joined
May 18, 2011
Messages
11
Firstly I would like to say well done to everyone who contributes to this forum. This website ranks in the top of google for excel related searches.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have been using the code from the post ‘VBA Consolidating Multiple Sheets in a Pivot Table’ successfully (http://www.mrexcel.com/forum/showthread.php?t=389230), (code pasted below) but I’ve run into an issue when I try to paste VBA code onto the end, that builds the finished pivot table. The ‘ActiveSheet.PivotTables’ name is PivotTable1 (see my macro recorder code below), but if I run the macro more than once, I get a compile error at that line where ActiveSheet.PivotTables "PivotTable1" first occurs (I think because the name has incremented by one ie. PivotTable2 etc).<o:p></o:p>


<o:p></o:p>
  • Could someone post a code amendment to make the macro compile successfully no matter how many times the macro is run?<o:p></o:p>
<o:p></o:p>
  • Could you also post code to make the name of the new sheet where the pivot table is created to be “Referrals”<o:p></o:p>
<o:p></o:p>
I’m using Excel 2003.
<o:p></o:p>
<o:p></o:p>
Thank you in advance for your assistance! :)

Stuart

---------------------<o:p></o:p>
<o:p></o:p>
Sub g_To_Be_Complet_Ref_table()<o:p></o:p>
<o:p></o:p>
'<o:p></o:p>
' Here’s the code from the post “VBA Consolidating Multiple Sheets in a Pivot Table”
' http://www.mrexcel.com/forum/showthread.php?t=389230
'<o:p></o:p>
<o:p></o:p>
Dim i As Long<o:p></o:p>
Dim arSQL() As String<o:p></o:p>
Dim objPivotCache As PivotCache<o:p></o:p>
Dim objRS As Object<o:p></o:p>
Dim wks As Worksheet<o:p></o:p>
Dim ws2 As Worksheet<o:p></o:p>
With ActiveWorkbook<o:p></o:p>
ReDim arSQL(1 To .Worksheets.Count)<o:p></o:p>
For Each wks In .Worksheets<o:p></o:p>
i = i + 1<o:p></o:p>
arSQL(i) = "SELECT * FROM [" & wks.Name & "$]"<o:p></o:p>
Next wks<o:p></o:p>
Set wks = Nothing<o:p></o:p>
Set objRS = CreateObject("ADODB.Recordset")<o:p></o:p>
<o:p></o:p>
objRS.Open Join$(arSQL, " UNION ALL "), _<o:p></o:p>
Join$(Array("Provider=Microsoft.jet.OLEDB.4.0; Data Source=", _<o:p></o:p>
.FullName, ";Extended Properties=""Excel 8.0;"""), vbNullString)<o:p></o:p>
Set objPivotCache = .PivotCaches.Add(xlExternal)<o:p></o:p>
Set objPivotCache.Recordset = objRS<o:p></o:p>
Set objRS = Nothing<o:p></o:p>
End With<o:p></o:p>
Set ws2 = Worksheets.Add<o:p></o:p>
With ws2<o:p></o:p>
objPivotCache.CreatePivotTable TableDestination:=.Range("A3")<o:p></o:p>
Set objPivotCache = Nothing<o:p></o:p>
.Range("A3").Select<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
'<o:p></o:p>
' Here’s my own code I made with the macro recorder, to create the finished pivot table<o:p></o:p>
' <o:p></o:p>
<o:p></o:p>
Range("B4").Select<o:p></o:p>
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Staff Responsible")<o:p></o:p>
.Orientation = xlRowField<o:p></o:p>
.Position = 1<o:p></o:p>
End With<o:p></o:p>
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Type")<o:p></o:p>
.Orientation = xlColumnField<o:p></o:p>
.Position = 1<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Start" _<o:p></o:p>
)<o:p></o:p>
.Orientation = xlColumnField<o:p></o:p>
.Position = 1<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _<o:p></o:p>
"PivotTable1").PivotFields("Last Name"), "Count of Last Name", xlCount<o:p></o:p>
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Type")<o:p></o:p>
<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the Board!

Throughout the code change:
ActiveSheet.PivotTables("PivotTable1")
to
ActiveSheet.PivotTables(1)
this will then reference the first PT on the worksheet rather than the one named "PivotTable1"

Immediately after:
With ws2<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

add
ws2.Name = "Referrals"

When posting code, use the code tags = it preserves indenting of the code and makes it easier to read. (See link in my sig)
 
Upvote 0
That amendment worked like a charm - thanks Phil. I appreciate your help.

I will use the code tags next time I post as well.

cheers,
Stuart :)
 
Upvote 0
Dear Excel and VBA experts!

This is a very elenant and well working piece of code.
However, it seems that it truncates to include all data a bit after line 12000.

Any known remedy for that?

Regards Jørgen Bugge
 
Upvote 0
I can see nothing in the code that would cause truncation. Please provide more details of your problem in a new post that references this one.
 
Upvote 0
Hi Phil,

Thanks for your answer. It triggered some suspicions that my code as derived from the example above had some flaws.

It proved out I had been wrong in using SELECT DISTINCT. Having removed the distinct, it's no problem anymore.

Thanks again! :)

BR, Jørgen
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top