• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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

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
Murray Brown
Asked:
Murray Brown
  • 3
1 Solution
 
Meir RivkinFull stack Software EngineerCommented:
what do u mean by go back to the state that it was in before?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
I gathered the cell(2,2).value then put it back.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
found a simple workaround
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now