Prevent Setting .CurrentPage to value that does not exist in PivotField.

Hello,

I have the following VBA to set the .CurrentPage (of the PivotTable Filter "Item Code") to a variable.


Sheets("Purchase Detail").PivotTables("PurchasePT").PivotFields("Item Code").CurrentPage = ItemCodeVariable

Open in new window


If ItemCodeVariable does not equal something that already exists in the the field "Item Code", I want it to fail and return a message. Currently, it will just set the page equal to the variable (effectively doing nothing except being confusing).

For example, "Item Code" would only be loaded with "100", "200","300","400". If I set ItemCodeVariable = 500, it should not work. (In reality, I have thousands of Item Codes).

Any help would be greatly appreciated.
EnigmaMatterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sub Macro()
bFound = False
Set pvtItems = Sheets("Purchase Detail").PivotTables("PurchasePT").PivotFields("Item Code").PivotItems
For Each pvtItem In pvtItems
    If pvtItem.Value = ItemCodeVariable Then
        bFound = True
        Exit For
    End If
Next
If bFound Then
    Sheets("Purchase Detail").PivotTables("PurchasePT").PivotFields("Item Code").CurrentPage = ItemCodeVariable
Else
    MsgBox "Error: Not Found"
End If
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rory ArchibaldCommented:
Branching out from Excelforum? ;)

You could also use a simple error handler:

Dim pi as pivotitem
with Sheets("Purchase Detail").PivotTables("PurchasePT").PivotFields("Item Code")
on error resume next
Set pi = .Pivotitems(ItemCodeVariable)
on error goto 0
if not pi is nothing then .CurrentPage = ItemCodeVariable
end with

Open in new window

0
EnigmaMatterAuthor Commented:
Rory,

ExcelForum is always the forum I recommend to others for Excel; I have never had a resource help me more in my entire college experience and career. But, I pay $25/Month for Experts Exchange, so I figured I ought to use it.

The one thing that Experts Exchange has is private questions -- makes me feel more comfortable talking because of the very proprietary nature of what I do.

I will try out these solutions!
0
EnigmaMatterAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.