Rowland Hamilton
Active Member
- Joined
- Nov 13, 2009
- Messages
- 250
Here is the code:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com
ffice
ffice" /><o
></o
>
<o
></o
>
This macro will update all pivot table page fields in a workbook to match the changes made to one pivot table. However, it will update all page fields with the value of the last cell selected before running it, no matter if it is the desired page field, a blank cell outside of the pivot table, or any other cell. How can I add a restriction and a message that will require and prompt the user to keep the desired page field highlighted before running the macro to update all page fields? If possible allow any pivot table page field to be a valid selection, but if necessary, make it the first pivot. (Note - I have only one page field per pivot, and 32 pivots).
Perhaps a forumula like this one I used in another context can be applied (Can't figure out how to modify this for my purposes):
Thank you, Rowland
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com




Code:
[COLOR=black][COLOR=black]Sub ChangeAllPivotHeaders()<o:p></o:p>[/COLOR]
[COLOR=black]Dim SPgField As String<o:p></o:p>[/COLOR]
[COLOR=black]'must keep cursor on page field<o:p></o:p>[/COLOR]
[COLOR=black]SPgField1 = Selection<o:p></o:p>[/COLOR]
[COLOR=black]Dim Pt As PivotTable<o:p></o:p>[/COLOR]
[COLOR=black]Dim Wsht As Worksheet<o:p></o:p>[/COLOR]
[COLOR=black]On Error Resume Next<o:p></o:p>[/COLOR]
[COLOR=black]For Each Wsht In ThisWorkbook.Worksheets<o:p></o:p>[/COLOR]
[COLOR=black]For Each Pt In Wsht.PivotTables<o:p></o:p>[/COLOR]
[COLOR=black]Pt.PageFields(1).CurrentPage = SPgField1<o:p></o:p>[/COLOR]
[COLOR=black]Next Pt<o:p></o:p>[/COLOR]
[COLOR=black]Next Wsht[/COLOR]
[COLOR=black]End Sub[/COLOR]
[/COLOR]


This macro will update all pivot table page fields in a workbook to match the changes made to one pivot table. However, it will update all page fields with the value of the last cell selected before running it, no matter if it is the desired page field, a blank cell outside of the pivot table, or any other cell. How can I add a restriction and a message that will require and prompt the user to keep the desired page field highlighted before running the macro to update all page fields? If possible allow any pivot table page field to be a valid selection, but if necessary, make it the first pivot. (Note - I have only one page field per pivot, and 32 pivots).
Perhaps a forumula like this one I used in another context can be applied (Can't figure out how to modify this for my purposes):
Code:
[COLOR=blue][COLOR=black]If Intersect(ActiveCell, Range("$F$8:$F$22")) Is Nothing Then[/COLOR]
[COLOR=black] Range("F8:F22").Select[/COLOR]
[COLOR=black] MsgBox "Please select a cell between F8 and F22"[/COLOR]
[COLOR=black]Else[/COLOR]
[/COLOR]
Thank you, Rowland