How do I check Pivot Table existence?

P

Paolo Sardi

Hi,

I'm trying to check if a pivot table named "MyPivot" exists in a given
worksheet.

I tried with:
If worksheets("Pivot").PivotTables("MyPivot") is nothing then...

Unfortunately Excel gives me an error, I think because it does not find the
PivotTables("MyPivot").

Is there a way to check if a PivotTable exists?

Thank you.
 
E

egun

How about using error trapping:

Dim Pivot_Found as Boolean

On Error Goto Pivot_Not_Found

Pivot_Found = False
If worksheets("Pivot").PivotTables("MyPivot") is nothing then...
....your code here...
Pivot_Found = True ' You made it here, so there was no error

Pivot_Not_Found:
If Not Pivot_Found Then
msgbox "Error: Pivot Table Not Found!"
Exit Sub
Endif

....continue with your code...

-=OR=-

You could test Activesheet.PivotTables.Count if you know there is only one
pivot table on the sheet. If it's zero, you know the pivot table isn't there.

HTH,

Eric
 
D

Dave Peterson

I'd use something like:

Dim PT as pivottable

set pt = nothing
on error resume next
set pt = worksheets("Pivot").PivotTables("MyPivot")
on error goto 0

if pt is nothing then
'nope
else
'yep
end if
 

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

Top