Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / November 2007

Tip: Looking for answers? Try searching our database.

Programmatically change an existing Pivot Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gussie - 01 Nov 2007 20:19 GMT
Hello to everyone, wondering if someone has figure out how to change existing
Pivot Tables. Once a pivot table is been created the SourceData is read only.
Creating a new pivot table using VBA is too complex and time-expensive.

Any suggestions?
George Nicholson - 01 Nov 2007 21:33 GMT
>Once a pivot table is been created the SourceData is read only.

Naah.

I've managed to update SourceData and refresh my pivot tables
programatically (a whole set of 20+ pivot tables, actually). If I remember
correctly, the hoop I had to jump through was making sure the string I fed
SourceData was in the *exact* correct format.  It's been a while since I
dealt with this, but below is (I hope) the pertinent code snippet. I hope
you might find it useful.

           ' Convert address of resized range back to R1C1 notation
               strNewRange = Application.ConvertFormula( _
                   Formula:="=" & rng.Address, _
                   fromReferenceStyle:=xlA1, _
                   toReferenceStyle:=xlR1C1)
               If Left(strNewRange, 1) = "=" Then strNewRange =
Mid(strNewRange, 2, Len(strNewRange) - 1)
               On Error Resume Next

               ' Final touches to range-address-as-string
               If Right(rng.Worksheet.Name, 1) = ")" Then
                   'Referenced sheet is a "copy", i.e., "Sheet1(2)" & needs
a leading aposthrophe (?)
                   strNewRange = "'" & rng.Worksheet.Name & "'!" &
strNewRange
               Else
                   strNewRange = rng.Worksheet.Name & "!" & strNewRange
               End If

               ' Apply range-address-as-string to pvt.SourceData
               If pvT.SourceData <> strNewRange Then
                   pvT.SourceData = strNewRange
               Else
                   pvT.RefreshTable
               End If

Alternatively, create named data ranges (i.e., "Pivot1Data"), and use that
name for the Pivot.Datasource value. Programatically you can then resize the
named range when appropriate and refresh the table.

Signature

HTH,
George

> Hello to everyone, wondering if someone has figure out how to change
> existing
[quoted text clipped - 3 lines]
>
> Any suggestions?
Gussie - 02 Nov 2007 12:38 GMT
I reviewed your code and I used what I needed. Ultimately I used what Roger
suggested. Maintaining a Named Ranged seemed a lot easier to me. Its been
very long since I developed an interface in excel -it has been challenging
but I am enjoying the contract.

Thank you guys for your time and code, hopefully I may be able to help you
next time.

> >Once a pivot table is been created the SourceData is read only.
>
[quoted text clipped - 44 lines]
> >
> > Any suggestions?
Roger Govier - 02 Nov 2007 00:20 GMT
Hi

Not sure exactly what you mean here. Creating a PT has no effect upon the
source data in terms of its Read/Write status.

If you are referring to the data source growing in size, then instead of
using a fixed range when creating the table, create a Dynamic Named Range.

Insert>Name>Define>
Name    MyData
Refers to    =OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1))

In the PT source dialogue, enter =MyData
Signature

Regards
Roger Govier

> Hello to everyone, wondering if someone has figure out how to change
> existing
[quoted text clipped - 3 lines]
>
> Any suggestions?

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.