My data should not expand beyond the W130, it is just that the data in
that area is changing constantly. Here is my macro but I am still
getting issues with it because I have four pivot tables in one
worksheet that is linked to different worksheets and I still get an
error. Please help.
Sub Auto_Open()
Application.ScreenUpdating = False
Sheets("Pivot").Visible = True
Sheets("Pivot").Select
Range("W3:W130").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
Sheets("Residential Data").Select
Range("W3:W130").Select
ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh
Sheets("HELOC Data").Select
Range("T3:T130").Select
ActiveSheet.PivotTables("PivotTable4").PivotCache.Refresh
Sheets("Commercial Data").Select
Range("T3:T130").Select
ActiveSheet.PivotTables("PivotTable5").PivotCache.Refresh
Sheets("Multifamily Data").Select
Sheets("Pivot").Visible = False
Application.ScreenUpdating = True
End Sub- Hide quoted text -
- Show quoted text -
Maggie,
Here is some code for you to test. I tried to comment the code for
your benefit, however, if you have questions please let me know. (I
will be honest though, this is the first time I have done any coding
with pivot tables). I created a mock-up pivot table in a workbook to
test this code and it seemed to work. I created the code as procedure
so that you can run it without having to fire the macro on the
workbook Open event. This way you can test the code without having to
open and close your worksheet many times.
I'm not sure if you are aware of the following or not (if you are,
then skip the paragraph): The F8 (Debug | Step Into) key will step
through code line by line and the F9 key will insert breaks (Debug |
Toggle Breakpoint) into the code (i.e. it will pause code execution on
the specified line). If you write "Debug.Print ..." into the code
window, it will write text to the Immediate Window (View | Immediate
Window). For example, after the line "For Each Wks In
ActiveWorkbook.Worksheets" you can insert a carriage return and write
Debug.Print "Worksheet Name:"; Wks.Name. As the code executes, it
will print the worksheet name into the Immediate Window. This is a
way of tracking what the program is doing.
Let me know if this helps.
Best,
Matt Herbert
Sub PivotTest()
Dim pvtCache As PivotCache
Dim pvtTable As PivotTable
Dim varShtArray As Variant
Dim Wks As Worksheet
Dim varWksMatch As Variant
'don't allow screen updating (this does NOT need to be set to TRUE
' at the end of the program; Excel will automatically set the
' property to TRUE at the end of the procedure's execution
Application.ScreenUpdating = False
'make the Pivot worksheet visible
Sheets("Pivot").Visible = True
'create an array with the worksheets that contain pivot tables to
' be updated
varShtArray = Array("Pivot", "Residential Data", "HELOC Data", _
"Commercial Data")
'loop through each worksheet in the workbook
For Each Wks In ActiveWorkbook.Worksheets
'use the match function to determine if the worksheet name
' matches a value in varShtArray; the variable is variant
' because the function will return an error if a match is
' not found
varWksMatch = Application.Match(Wks.Name, varShtArray, 0)
'proceed if varWksMatch is not an error (i.e. a match was found)
If Not IsError(varWksMatch) Then
'loop through each pivot table on the worksheet
For Each pvtTable In Wks.PivotTables
'refresh the pivot table
pvtTable.PivotCache.Refresh
Next
End If
Next
'when the screen updating returns, start the user on the
' Multifamily Data worksheet
Sheets("Multifamily Data").Select
'hide the Pivot worksheet
Sheets("Pivot").Visible = False
End Sub