updating a pivot tables connection through code

star1984

New Member
Joined
May 12, 2011
Messages
19
Hi,

Firstly I am not very familiar with VBA but I am trying to learn.

I have a piece of code that several Excel workbooks call. This updates the various connections with a centrally stored connection string SQLConn.

I have the following code for updaing query tables and pivot tables. The query table part always works, however the pivot table part sometimes works and sometimes doesn't. Sometimes the code will crash as a pivottable will have no pivotcache or connection property. Does anyone know why this would be? Is there one way to access a pivot tables connection property? please help

For Each Worksheet In callingworkbook.Worksheets
itemcount = 0
For Each Item In Worksheet.QueryTables
itemcount = itemcount + 1
Worksheet.QueryTables.Item(itemcount).Connection = SQLConn
Next

itemcount = 0
For Each PivotTable In Worksheet.PivotTables
itemcount = itemcount + 1
Worksheet.PivotTables(itemcount).PivotCache.Connection = SQLConn
Next
Next
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I replace my pivotcache.connection code with the following and it seems to work for everything. May not be the best way of doing (I really don't know!) but all I know is it works

For Each PivotTable In Worksheet.PivotTables
callingworkbook.Connections(itemcount).ODBCConnection.SavePassword = True
callingworkbook.Connections(itemcount).ODBCConnection.Connection = SQLConn
callingworkbook.Connections(itemcount).ODBCConnection.SavePassword = False
Next
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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