Incomprehensible or impossible?

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
Can you help please with the following query?
The code below is an excellent way to update a pivot table from another sheet but if I choose a value that doesn't exist in the pivot the data in the pivot is overwritten.

My code:


Private Sub Worksheet_Change(ByVal Target As Range)
Dim PT1 As PivotTable
If Not Intersect(Target, Range("B7")) Is Nothing Then
Set PT1 = Sheets("PIVOT").PivotTables("MainPivotTable")
PT1.PivotFields("Team").CurrentPage = Sheets("Summary").Range("B7").Value
Set PT1 = Nothing
End If


What does the code do?
The value in Cell B7 updates the Page field of the Pivot Table "MainPivotTable"

My query
What about If Cell B7contains a value that does not exist in the pivot table? Is there a way to get a message box saying "value doesn't exist"?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi,
before:
PT1.PivotFields("Team").CurrentPage = Sheets("Summary").Range("B7").Value

Put something like:

FoundPI = False
'Loop through all the items
For Each PI In PT1.PivotFields("Team").PivotItems
If PI.Value = Sheets("Summary").Range("B7").Value
'Found a match!
FoundPI = True
Exit For
Else
EndIf
Next PI

If FoundPI = True Then
PT1.PivotFields("Team").CurrentPage = Sheets("Summary").Range("B7").Value
Else
'Message to the user?
EndIf

Cheers,
Koen
 
Upvote 0
thanks for your answer.

There is another way as well suggested by RS411 in my previous thead " If Not Intersect Target and Pivot Table"

Cheers
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top