If the values in Column A are text...
='sheet name'!$A$1:INDEX('sheet name'!$1:$65536,MATCH("ZZZZZZ",'sheet name'!$A:$A),MATCH("ZZZZZZ",'sheet name'!$1:$1))
If the values in Column A are numeric/dates...
='sheet name'!$A$1:INDEX('sheet name'!$1:$65536,MATCH(10^300,'sheet name'!$A:$A),MATCH("ZZZZZZ",'sheet name'!$1:$1))
If the values in Column A are text...
='sheet name'!$A$1:INDEX('sheet name'!$1:$1048576,MATCH("ZZZZZZ",'sheet name'!$A:$A),
MATCH("ZZZZZZ",'sheet name'!$1:$1))
If the values in Column A are numeric/dates...
='sheet name'!$A$1:INDEX('sheet name'!$1:$1048576,MATCH(10^300,'sheet name'!$A:$A),
MATCH("ZZZZZZ",'sheet name'!$1:$1))
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
' By Patrick G. Matthews
' Feel free to use this code anywhere, as long as you attribute authorship and the URL where you
' found it
' This event sub fires every time a sheet is selected. The intent is to force a refresh of any
' PivotTables/PivotCharts on that sheet, as it is possible that the source data may have changed.
' Thus, this event attempts to ensure that the PT/PC is always in sync with its source data.
' Use care with this code if the data source is external; the frequent refreshes may not not be
' appropriate in that instance
' Code assumes that the source data and the PivotTable/PivotChart are never on the same worksheet!
' If the PT/PC are on the same worksheet as the source data, changes to the source data are NOT
' automatically reflected until the Workbook_SheetActivate event fires again
Dim PT As PivotTable
Dim ChtObj As ChartObject
Dim Cht As Chart
' Based on the type of sheet activated (Worksheet, Chart, Excel4MacroSheet), run the appropriate
' code. Please note that the Type property for a Chart will actually return the same value as
' for Excel4Macro sheets:
' http://excel.tips.net/Pages/T002538_Detecting_Types_of_Sheets_in_VBA.html
Select Case Sh.Type
Case xlWorksheet
' Loop through any PTs present on worksheet and refresh
For Each PT In Sh.PivotTables
PT.RefreshTable
Next
' Loop through ChartObjects on worksheet. If any are PivotCharts, refresh the underlying
' PT (which will also refresh the PC)
For Each ChtObj In Sh.ChartObjects
If Not ChtObj.Chart.PivotLayout Is Nothing Then
ChtObj.Chart.PivotLayout.PivotTable.RefreshTable
End If
Next
Case Else
' As above, for Chart sheets, the Type property returns the same value as for Excel4Macro
' sheets. Since this branch of the code gets both Chart and Excel4Macro sheets, we use
' the loop to avoid the error that would occur if we just tested Sh.PivotLayout directly.
' If the sheet really is a Chart, and if that Chart is a PC, then refresh the underlying
' PT (and thus the PC itself)
For Each Cht In Me.Charts
If Cht.Name = Sh.Name Then
If Not Sh.PivotLayout Is Nothing Then
Sh.PivotLayout.PivotTable.RefreshTable
End If
Exit For
End If
Next
End Select
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim PC As PivotCache
For Each PC In Me.PivotCaches
PC.Refresh
Next
End Sub
Sub CreateName_PivotRange()
Dim MakeName As String
Dim RefersTo As String
Dim Numbers As Long
MakeName = InputBox("What do you want to use for the Name?", "Create Dynamic Name", "PivotRange")
If Trim(MakeName) = "" Then
MsgBox "You did not enter a valid Name", vbCritical, "Aborting"
Exit Sub
End If
Numbers = MsgBox("Are the values in Column A numeric or dates? If yes, click Yes", vbQuestion + vbYesNo, _
"Data Type")
On Error GoTo ErrHandler
If Numbers = vbYes Then
RefersTo = "='" & ActiveSheet.Name & "'!$A$1:INDEX('" & ActiveSheet.Name & "'!$1:$" & ActiveSheet.Rows.Count & _
",MATCH(10^300,'" & ActiveSheet.Name & "'!$A:$A),MATCH(""ZZZZZZZZ"",'" & ActiveSheet.Name & "'!$1:$1))"
Else
RefersTo = "='" & ActiveSheet.Name & "'!$A$1:INDEX('" & ActiveSheet.Name & "'!$1:$" & ActiveSheet.Rows.Count & _
",MATCH(""ZZZZZZZZ"",'" & ActiveSheet.Name & "'!$A:$A),MATCH(""ZZZZZZZZ"",'" & ActiveSheet.Name & "'!$1:$1))"
End If
ActiveWorkbook.Names.Add MakeName, RefersTo
Exit Sub
ErrHandler:
MsgBox "Error #" & Err.Number & ", " & Err.Description, vbCritical, "Name Not Created"
End Sub
Sub AddRefreshPTEventSub()
' This sub creates a Workbook_SheetActivate event sub useful for enforcing PivotTable/PivotChart
' updates on sheet selection. The code creates the event sub in the currently active workbook.
' For this code to work, "Trust access to VB Project" must be enabled. In Excel 2003 and 2002,
' to enable this, go to Tools|Macro|Security in the menu, and on the Trusted Publishers tab, check
' Trust Access to Visual Basic Project.
' In Excel 2007, click the Office button, and click Excel Options. Select Trust Center, and click
' Trust Center Settings. Check the box for Trust access to the VBA project object model
Dim Line As Long
Dim arr(0 To 62) As String
' Populate array with the code for the sub, without the first line or the End Sub line
arr(0) = " "
arr(1) = " ' By Patrick G. Matthews"
arr(2) = " "
arr(3) = " ' Feel free to use this code anywhere, as long as you attribute authorship and the URL where you"
arr(4) = " ' found it"
arr(5) = " "
arr(6) = " ' This event sub fires every time a sheet is selected. The intent is to force a refresh of any"
arr(7) = " ' PivotTables/PivotCharts on that sheet, as it is possible that the source data may have changed."
arr(8) = " ' Thus this event attempts to ensure that the PT/PC is always in sync with its source data."
arr(9) = " "
arr(10) = " ' Use care with this code if the data source is external; the frequent refreshes may not not be"
arr(11) = " ' appropriate in that instance"
arr(12) = " "
arr(13) = " ' Code assumes that the source data and the PivotTable/PivotChart are never on the same worksheet!"
arr(14) = " ' If the PT/PC are on the same worksheet as the source data, changes to the source data are NOT"
arr(15) = " ' automatically reflected until the Workbook_SheetActivate event fires again"
arr(16) = " "
arr(17) = " Dim PT As PivotTable"
arr(18) = " Dim ChtObj As ChartObject"
arr(19) = " Dim Cht As Chart"
arr(20) = " "
arr(21) = " ' Based on the type of sheet activated (Worksheet, Chart, Excel4MacroSheet), run the appropriate"
arr(22) = " ' code. Please note that the Type property for a Chart will actually return the same value as"
arr(23) = " ' for Excel4Macro sheets:"
arr(24) = " ' http://excel.tips.net/Pages/T002538_Detecting_Types_of_Sheets_in_VBA.html"
arr(25) = " "
arr(26) = " Select Case Sh.Type"
arr(27) = " Case xlWorksheet"
arr(28) = " "
arr(29) = " ' Loop through any PTs present on worksheet and refresh"
arr(30) = " "
arr(31) = " For Each PT In Sh.PivotTables"
arr(32) = " PT.RefreshTable"
arr(33) = " Next"
arr(34) = " "
arr(35) = " ' Loop through ChartObjects on worksheet. If any are PivotCharts, refresh the underlying"
arr(36) = " ' PT (which will also refresh the PC)"
arr(37) = " "
arr(38) = " For Each ChtObj In Sh.ChartObjects"
arr(39) = " If Not ChtObj.Chart.PivotLayout Is Nothing Then"
arr(40) = " ChtObj.Chart.PivotLayout.PivotTable.RefreshTable"
arr(41) = " End If"
arr(42) = " Next"
arr(43) = " "
arr(44) = " Case Else"
arr(45) = " "
arr(46) = " ' As above, for Chart sheets, the Type property returns the same value as for Excel4Macro"
arr(47) = " ' sheets. Since this branch of the code gets both Chart and Excel4Macro sheets, we use"
arr(48) = " ' the loop to avoid the error that would occur if we just tested Sh.PivotLayout directly."
arr(49) = " "
arr(50) = " ' If the sheet really is a Chart, and if that Chart is a PC, then refresh the underlying"
arr(51) = " ' PT (and thus the PC itself)"
arr(52) = " "
arr(53) = " For Each Cht In Me.Charts"
arr(54) = " If Cht.Name = Sh.Name Then"
arr(55) = " If Not Sh.PivotLayout Is Nothing Then"
arr(56) = " Sh.PivotLayout.PivotTable.RefreshTable"
arr(57) = " End If"
arr(58) = " Exit For"
arr(59) = " End If"
arr(60) = " Next"
arr(61) = " End Select"
arr(62) = " "
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
' Create a "stub" for the event sub. Line captures the line number in the module for the first line
Line = .CreateEventProc("SheetActivate", "Workbook")
' Advance the line count
Line = Line + 1
' Insert the lines from the array
.InsertLines Line, Join(arr, vbCrLf)
.DeleteLines Line + UBound(arr) + 1
End With
End Sub
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (8)
Commented:
Great article! As a "novice" I will need to play with this today to get it to work and for me to understand. While the article is excellent, I do not have access (when I click on it to the sample XLSM file. Can you send it to me or tell me where I can go to find it??
Thank you,
B.
Author
Commented:So glad you liked the article! I just tested the download links for both sample files using both IE 8 and FireFox 10, and they work for me, so I am not sure what the problem is.
I can email you the files. Drop me a line; my email address is available in the About Me tab of my profile.
Patrick
Commented:
Commented:
Commented:
In Excel 2010 I have this firing on a button click, and it's running like a champ, but the refresh only occurs when I'm doing step-through debugging.
View More