Solved

Excel 2007 VB to Automatically Refresh a Pivot Table on Sheet 2 when a specific cell from Sheet 1 Changes

Posted on 2011-02-10
3
656 Views
Last Modified: 2012-05-11
Hello all.

I've been working on an Excel 2007 WB and have had a lot of great help from many.  What I am now attempting to do is to Auto refresh a Pivot Table when data in a specific cell is changed.  The input is on one sheet and the Pivot table is on a different sheet all in the same workbook.

I found an earlier (2009) thread and attempted it but it is not working.  Here is what my background  is

Clerk inputs info in worksheet "Payroll Data Input".  In cell "W1" of that worksheet a validation list shows the available years.  That list is based upon a Pivot table on worksheet "Pay Dates" it is PivotTable2 which starts in cell o4.

What I want it to do is to refresh the data in the Pivot Table when the clerk selects what year they are working on.

Below is the code from an earlier thread here.  Anyone see why it isn't working.

Private Sub Worksheet_Change(ByVal Target As Range)
   Dim pc As PivotCache
   For Each pc In ActiveWorkbook.PivotCaches
      pc.Refresh
   Next pc
End Sub

Open in new window

0
Comment
Question by:wlwebb
3 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34867127
You can refresh all pivotables as below without looping

Do you have a sample file, it appears that your code could be more tailored to a specific cell change and specific pivotttable

Cheers

Dave
ActiveWorkbook.RefreshAll

Open in new window

0
 
LVL 9

Accepted Solution

by:
hitsdoshi1 earned 500 total points
ID: 34867132
Your code will trigger refresh whenever any cell is change, I think what you need to is the code I attached. You just want the pivot refresh when someone changes the year. This should do the job.

Good Luck!
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Name = "Payroll Data Input" Then
    If Target.Address = "$W$1" Then
        myvalue = Sheets("Payroll Data Input").Range("W1")
        Sheets("Pay Dates").PivotTables("PT2").PivotFields("Pay Date Calender Year").ClearAllFilters
        Sheets("Pay Dates").PivotTables("PT2").PivotFields("Pay Date Calender Year").CurrentPage = myvalue
    End If
End If
End Sub

Open in new window

0
 

Author Closing Comment

by:wlwebb
ID: 34869083
Perfection.  Wow you guys were quick!  I posted this and went to dinner.  Didn't think I'd have an answer that fast.  Thanks so very much!
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I comeā€¦
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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.

773 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