How do I check Pivot Table existence?

  • Thread starter Thread starter Paolo Sardi
  • Start date Start date
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.
 
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
 
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
 
Back
Top