Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Change to SQL of Pivot Table on shared data

If I write a couple of line of code to change pivot table datasources (we
are changing backends) it works perfectly UNTIL more than one pivot table
looks at the same data source.

This is easily recreatable by adding a pivot table on, say, northwind.mdb.
Run the code below (ignore the connection) then adding a second pivot table
and running it again.

In fact, the error is recreateable just by setting it to the existing SQL!

Has anybody got round this successfully or knows what I am doing wrong?

Cheers!
Simon


'Change pivot table data connections
Dim stSQL as String
Dim
Dim PC As PivotCache
For Each PC In ActiveWorkbook.PivotCaches
'PC.Connection = SplitToArray(PC.Connection) 'No need to change for
testing
stSQL = PC.Sql
stSQL = stSQL 'No need to even change the SQL for testing
PC.Sql = stSQL
MsgBox "Done"
Next

Function Replacestr(TextIn, SearchStr, Replacement, CompMode As Integer)
'Trad - poss Getz
Dim WorkText As String, Pointer As Integer
If IsNull(TextIn) Then
Replacestr = Null
Else
WorkText = TextIn
Pointer = InStr(1, WorkText, SearchStr, CompMode)
Do While Pointer 0
WorkText = Left(WorkText, Pointer - 1) & Replacement & Mid(WorkText,
Pointer + Len(SearchStr))
Pointer = InStr(Pointer + Len(Replacement), WorkText, SearchStr,
CompMode)
Loop
Replacestr = WorkText
End If
End Function

Function SplitToArray(ST As String, Lump As Integer)
'Stolen from newsgroup by way of google
' break a long string up into an array with each element of size Lump
' don't bother if string is not longer than Lump
Dim A()
Dim I As Integer
If Len(ST) <= Lump Then
SplitToArray = ST
Else
ReDim A(1 To Len(ST) \ Lump + 1)
For I = 1 To Len(ST) \ Lump + 1
A(I) = Mid(ST, 1 + (I - 1) * Lump, Lump)
Next
SplitToArray = A()
End If
End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Change to SQL of Pivot Table on shared data

Simon,

I use Define Name to give the data range a name. Than I have my Pivot
Table refer to the name as the data source.

It is easy to have code recreate name and change the Refers to: portion .
(In my use the names are self-expanding so that if the ranges increase or
decrease, the Pivot Tables adjust accordingly.)

If you are changing the actual source of the data, be sure that the headers
are the same.

--
sb
"Simon" wrote in message
...
If I write a couple of line of code to change pivot table datasources (we
are changing backends) it works perfectly UNTIL more than one pivot table
looks at the same data source.

This is easily recreatable by adding a pivot table on, say, northwind.mdb.
Run the code below (ignore the connection) then adding a second pivot

table
and running it again.

In fact, the error is recreateable just by setting it to the existing SQL!

Has anybody got round this successfully or knows what I am doing wrong?

Cheers!
Simon


'Change pivot table data connections
Dim stSQL as String
Dim
Dim PC As PivotCache
For Each PC In ActiveWorkbook.PivotCaches
'PC.Connection = SplitToArray(PC.Connection) 'No need to change

for
testing
stSQL = PC.Sql
stSQL = stSQL 'No need to even change the SQL for testing
PC.Sql = stSQL
MsgBox "Done"
Next

Function Replacestr(TextIn, SearchStr, Replacement, CompMode As Integer)
'Trad - poss Getz
Dim WorkText As String, Pointer As Integer
If IsNull(TextIn) Then
Replacestr = Null
Else
WorkText = TextIn
Pointer = InStr(1, WorkText, SearchStr, CompMode)
Do While Pointer 0
WorkText = Left(WorkText, Pointer - 1) & Replacement & Mid(WorkText,
Pointer + Len(SearchStr))
Pointer = InStr(Pointer + Len(Replacement), WorkText, SearchStr,
CompMode)
Loop
Replacestr = WorkText
End If
End Function

Function SplitToArray(ST As String, Lump As Integer)
'Stolen from newsgroup by way of google
' break a long string up into an array with each element of size Lump
' don't bother if string is not longer than Lump
Dim A()
Dim I As Integer
If Len(ST) <= Lump Then
SplitToArray = ST
Else
ReDim A(1 To Len(ST) \ Lump + 1)
For I = 1 To Len(ST) \ Lump + 1
A(I) = Mid(ST, 1 + (I - 1) * Lump, Lump)
Next
SplitToArray = A()
End If
End Function




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pivot table change data source mcarter Excel Discussion (Misc queries) 0 September 14th 09 06:14 PM
Can't change Pivot Table data source AABob Excel Worksheet Functions 1 January 30th 09 07:07 PM
can i change pivot table to change data jacob22 Excel Discussion (Misc queries) 2 April 11th 07 03:32 PM
change data source of pivot table JohnH Excel Discussion (Misc queries) 9 September 15th 06 12:52 AM
Change Data In Pivot Table John Calder New Users to Excel 1 July 7th 05 10:41 PM


All times are GMT +1. The time now is 12:14 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"