VBA: Refreshing Pivot Table with external data connection

levo

New Member
Joined
Jul 8, 2011
Messages
5
In my Excel (Excel 2007 SP2) file I use 2 data sources/connections (let's call them A and B) that are linked to SQL server data.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Both data sources are in separated worksheets in the same file.
<o:p></o:p>
<o:p></o:p>
There is another connection (C) that refers to data sources A,B and creates/selects data for creating Pivot Table.<o:p></o:p>
I can create this Pivot Table using external connection C and can refresh Pivot Table manually.
<o:p></o:p>
<o:p></o:p>
When trying to refresh Pivot Table using VBA code I receive this Visual Basic error: <o:p></o:p>
"Run Time error'1004' [Microsoft][ODBC Excl Driver] The connection for viewing your linked Microsoft Excel worksheet was lost"
<o:p></o:p>
<o:p></o:p>
To refresh Pivot table I tried using <o:p></o:p>
Code:
[COLOR=black][FONT=Verdana]ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh<o:p></o:p>[/FONT][/COLOR]
<o:p></o:p>
then also <o:p></o:p>
Code:
[COLOR=black][FONT=Verdana]ActiveSheet.PivotTables("PivotTable2").RefreshTable<o:p></o:p>[/FONT][/COLOR]
<o:p></o:p>
but didn't helped. Still got the same error. <o:p></o:p>
<o:p></o:p>
If I use connection C for getting data to new worksheet, then create Pivot and use the same code to refresh Pivot it works.
Point is that I don’t want to create another data worksheet because it increases file size.

Do I use wrong code or is it impossible to refresh pivot table from external data source using code?

Thank you in advance for any ideas.<o:p></o:p>
<o:p></o:p>
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,224,521
Messages
6,179,286
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