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

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

wlwebbAsked:
Who is Participating?
 
hitsdoshi1Commented:
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
 
DaveCommented:
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
 
wlwebbAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.