Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net/VBA Pivot table refreshes in different form/filter

Posted on 2013-01-23
4
Medium Priority
?
329 Views
Last Modified: 2013-02-03
Hi

 In my Excel Add-in I use the following code in refreshing a pivot table after
changes to the data behind it are made. The problem is that the pivot table
doesn't go back to the state that it was in before. Is there a way to stop this?


Sub ChangePivotSource()
        Try
            Dim oPT As Excel.PivotTable
            Dim oPC As Excel.PivotCache

            Globals.ThisAddIn.Application.Sheets("Pivot").activate()
            oPT = Globals.ThisAddIn.Application.ActiveSheet.PivotTables(1)

            oPC = oPT.PivotCache
            oPC.SourceData = "Data!" & Globals.ThisAddIn.Application.ConvertFormula("$A$1:$F$90", Excel.XlReferenceStyle.xlA1, Excel.XlReferenceStyle.xlR1C1)


            oPT.RefreshTable()

            If Not oPT Is Nothing Then oPT = Nothing

            If Not oPC Is Nothing Then oPC = Nothing

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
0
Comment
Question by:Murray Brown
  • 3
4 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 38813331
what do u mean by go back to the state that it was in before?
0
 

Author Comment

by:Murray Brown
ID: 38813345
When I open the saved Excel sheet the pivot looks like the first image.
I then go to the second image, make my changes, and hit the button that
refreshes the pivot table with the data that I have changed.
After the refresh the pivot goes back to the state it is in in the first image,
when I need it to go back to the second image

12
0
 

Accepted Solution

by:
Murray Brown earned 0 total points
ID: 38831566
I gathered the cell(2,2).value then put it back.
0
 

Author Closing Comment

by:Murray Brown
ID: 38848275
found a simple workaround
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question