Dim i As Long
With WS.PT.PF
For i = 1 To .PivotItems.Count
If .PivotItems(i) = OptionX Then
Dim SPgField1 As Variant '!!!!!Move out of Loop!!!!!
SPgField1 = OptionX
.CurrentPage = SPgField1
MsgBox ("OK / i=" & i)
SetPivotFieldFunction True
Else
SetPivotFieldFunction False
End If
Next i
End With
End Function
Dim statements should ALWAYS be at the begining of code.
Die_Another_Day
s80NL wrote:
Absolutely awesome
I've created to following code out of your
remarks, but I've to wait till tomorrow morning to test it at work...
so I'll let you know the results later.
How silly can it be Value instead of Select... Today, I've been staring
at that stupid line for ages 
Code:
--------------------
Sub ShouldWorkNow()
' NOT TESTED YET... will test it tomorrow at work :)
Dim i As Long
With Sheets("Aflopende contracten").PivotTables("PivotAFL").PivotFields(" AM")
For i = 1 To .PivotItems.Count
If .PivotItems(i) = s1.Range("D2").Value Then
Dim SPgField1 As Variant
s1.Range("D2").Select
SPgField1 = Selection
.CurrentPage = SPgField1
End If
Next i
End With
End Sub
--------------------
The second part of my quest to make it dynamicly was to loop through
all the values on Sheet("Inhoud") where I manage the default values. I
think this should do the trick, but just as above, I haven't tested it
yet!
Code:
--------------------
Function SetPivotFieldFunction(WS, PT, PF, OptionX) As Boolean
'NOT TESTED YET... will test tomorrow at work :)
Dim i As Long
With WS.PT.PF
For i = 1 To .PivotItems.Count
If .PivotItems(i) = OptionX Then
Dim SPgField1 As Variant
SPgField1 = OptionX
.CurrentPage = SPgField1
MsgBox ("OK / i=" & i)
SetPivotFieldFunction True
Else
SetPivotFieldFunction False
End If
Next i
End With
End Function
--------------------
Code:
--------------------
Sub SetPivotField()
'NOT TESTED YET... will test tomorrow at work :)
Dim i As Long
Dim WS As Worksheet
Dim PT As PivotTable
Dim PF As PivotField
Dim OptionX As String
For i = 1 To 10
WS = s1.Range("A" & i)
PT = s1.Range("B" & i)
PF = s1.Range("C" & i)
OptionX = s1.Range("D" & i)
If SetPivotFfieldFunction(WS, PT, PF, OptionX) = True Then
MsgBox "True... " & PF & "." & OptionX, vbInformation
Else
MsgBox "False... " & PF & "." & OptionX, vbCritical
End If
Next i
End Sub
--------------------
If you see anything that should be changed before I run my test
tomorrow, please let me know in advance.
--
s80NL
------------------------------------------------------------------------
s80NL's Profile: http://www.excelforum.com/member.php...o&userid=36374
View this thread: http://www.excelforum.com/showthread...hreadid=561968