Chart object effect on Dim sht As Worksheet

G

Guest

I have a macro that refreshes Pivot Tables by looking for worksheets with
names beginning with HALGL and then refreshes pivot tables within those
sheets. The macro works great until I insert a chart object. Then the macro
is unable to evaluate:
For Each sht In ActiveWorkbook.Sheets
....
Next sht

Why is this? What is the solution???

Thanks, in advance, for your help!

My code:
Sub mcrRefreshPivot()
Dim sht As Worksheet
Dim pvt As PivotTable
Dim i As Integer
Dim pf As PivotField
Dim pi As PivotItem

'Turn calculation off during pivot refresh and month selection process.
Application.Calculation = xlManual

'Refresh the Main Pivot Tables.
'Since other pivot tables are based on the main table, this will refresh
them, as well.
On Error GoTo ErrorHandler

'Sheets("HALGL...").PivotTables("pvt_Ref...").RefreshTable

For Each sht In ActiveWorkbook.Sheets
If Left(sht.Name, 5) = "HALGL" Then
For i = 1 To sht.PivotTables.count
Set pvt = sht.PivotTables(i)
If Left(pvt.Name, 7) = "pvt_Ref" Then
pvt.RefreshTable
End If
Next i
End If
Next sht
 
A

Andy Pope

Hi,

Use the worksheet collection instead of the Sheets collection to
restrict the type of sheet.

For Each sht In ActiveWorkbook.WorkSheets
....
Next sht

You declared the variable sht as a worksheet object so the Chart sheet
would give a type mis-match error.
Also using the .Sheets collection will return all sheets within the
workbook. This would include worksheets and chart sheets.

Cheers
Andy
 
G

Guest

Awesome! Thanks!

Andy Pope said:
Hi,

Use the worksheet collection instead of the Sheets collection to
restrict the type of sheet.

For Each sht In ActiveWorkbook.WorkSheets
....
Next sht

You declared the variable sht as a worksheet object so the Chart sheet
would give a type mis-match error.
Also using the .Sheets collection will return all sheets within the
workbook. This would include worksheets and chart sheets.

Cheers
Andy
 

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