Querying workbook and creating pivot table works in 2007 but not in 2010

cjrberg

New Member
Joined
Aug 28, 2011
Messages
1
Hi all, my first post in the forum for a problem that has annoying me for some time now. I am trying to take the union between different sheets to consolidate the data and then create a pivot table that can be refreshed by a click of a button. I have something that works in Excel 2007 but does not in Excel 2010 for some reason I do not understand.

Option Explicit

Sub Create_Pivot_Source()
'Credit to XL-Dennis for the original code ideas.

Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stCon As String, stSQL As String
Dim i As Long, lnMode As Long
Dim pvc As PivotCache
Dim wbBook As Workbook
Dim wsSheet As Worksheet

Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Blad1")



stCon = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & wbBook.FullName & ";" & _
"Extended Properties=""Excel 12.0;HDR=NO"";"





stSQL = "SELECT * FROM [Blad2$] UNION ALL SELECT * FROM [Blad3$]"


Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset

cnt.Open stCon
rst.Open stSQL, cnt, adOpenStatic, adLockOptimistic

Set pvc = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
Set pvc = Worksheets("Blad1").PivotTables("Pivottabell29").PivotCache
Set pvc.Recordset = rst

I have a pivot table named 29 but I get an error on the last row, after that I thought I should be able to just use refresh and have everything working alright.

I would appreciate if someone has any ideas on this also some references to books where I can find the good information on these things as it seems quite hard to get a detailed description and such also a lot of books are old do not know if they cover the latest 2010 version and such.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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