Finding CurrentPage in PivotTable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

Is there a way to test to see if an Item is in the list of PivotField,
before setting the CurrentPage to that item? The reason I ask is if you don't
check first to make sure the item exists, then set the CurrentPage to the
item you want, it creates that item for you, which is not what I want it to
do.

Cheers,
Grant.

ActiveWorkbook.Worksheets(wkSheet.Name).PivotTables(pvtTable.Name).PivotFields("Line").CurrentPage = sStr
 
You can check if the item exists:

'========================
Sub ChangePivotPage()
On Error Resume Next
Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim str As String
Set ws = ActiveSheet
Set pt = ws.PivotTables(1)
str = ws.Range("E2").Value

With pt.PageFields("Rep")
'Test if Item exists
Set pi = .PivotItems(str)
On Error GoTo 0
If pi Is Nothing Then
MsgBox str & " is not an item in the Rep field"
Else
.CurrentPage = str
End If
End With

End Sub
'============================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top