View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Hans Hans is offline
external usenet poster
 
Posts: 25
Default Changing field in Pivot Table with macro

I have a problem with the macro below that makes the new
period (mentioned in my sheet OVERVIEW, cell E7) visible
in a number of Pivot Tables.

For some reason, the value of 'Worksheets("OVERVIEW").Range
("E7").Value' is not assigned to i. When I query the 'i =
Worksheets("OVERVIEW").Range("E7").Value' statement in the
immediate window, i get False as answer. However, when I
only query 'Worksheets("OVERVIEW").Range("E7").Value' I
get 2, the correct value of our current period.

Any idea what I am doing wrong?

regards,
Hans



Sub PivotChangePeriod()

Dim i As Long
Dim PT As PivotTable
Dim PI As PivotItem
Dim Sh1 As Sheets
Dim wk As Worksheet

i = Worksheets("OVERVIEW").Range("E7").Value
Set Sh1 = Worksheets(Array("ALEN", "ALEN (2)"))

For Each wk In Sh1
For Each PT In wk.PivotTables
PT.PivotFields("period").PivotItem(i).Visible
= True
Next PT
Next wk

End Sub