If Pivot Table Exists

G

Guest

I am looking for VBA code that simply returns a boolean value based on
whether a pivot table exists in a worksheet. Code I have come up with is
below:-

Dim ws as Worksheet
Dim PivotReport = PivotTable
If ws.PivotTables("PivotTable3") Is Nothing = False Then 'ie the
pivot table exists
Set PivotReport = ws.PivotTables("PivotTable3")
End If

When I run this I get a "1001 object defined run time error". It appears
the "Is Nothing" method is not right here. Is there an "Exists" or other
like function for determining the existence of a pivot table in a Worksheet?

Thanks Team
 
G

Guest

You do not specify what ws is.

Dim ws as Worksheet
Dim PivotReport as PivotTable

set ws = activesheet

If not ws.PivotTables("PivotTable3") Is Nothing Then
Set PivotReport = ws.PivotTables("PivotTable3")
End If
 
G

Guest

Thanks for that but even with

Set ws = ActiveSheet

it doesn't work. Any other ideas?

ta
 
P

papou

Hello
Amend your code as follows:
Dim ws as Worksheet
Dim PivotReport As PivotTable
If Not ws.PivotTables("PivotTable3") Is Nothing Then
Set PivotReport = ws.PivotTables("PivotTable3")

HTH
Cordially
Pascal
 
G

Guest

Dim ws as Worksheet
Dim PivotReport as PivotTable

set ws = activesheet
On error resume next
Set PivotReport = ws.PivotTables("PivotTable3")
On error Goto 0
if not pivotreport is nothing then

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