Run-time error '1004'

amrita17170909

Board Regular
Joined
Dec 11, 2019
Messages
74
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
Hi All,

The code is simply to refresh a pivot chart.

I recently introduced an error handler in the code to make it sturdier. I am getting an error as per below:

Run-time error '1004'
Unable to get the PivotTables property of the Worksheet class

VBA Code:
Public Sub PBL_SUB_Refresh_Pivotchart()

' Refresh Pivot chart in "Table 1" tab refreshes the data used to calculate the pivot chart
ActiveWorkbook.RefreshAll

With ActiveSheet.PivotTables("PivotTable6").PivotFields("BRN Description")
        .PivotItems("0").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
    
ErrHandler:
Debug.Print Err.Number, Err.Description, Err.Source
MsgBox "Error " & Err.Number & ": " & Err.Description & Chr(13) & "From: " & Err.Source & Chr(13) & "Function : " & "PBL_SUB_Refresh_Pivotchart"

Resume Next

End Sub

Can someone advise what am I doing wrong ?

Amrita
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The only cause I can think of is the activesheet is NOT the sheet where the requested pivottable actual is located.
 
Upvote 0
Yep included the activesheet and it works now

My errorhandler still comes up with a run-time error .
 
Upvote 0
Having an error handler does not sturdier, I consider it weaker. It is better to try to validate the possible error.

The code would look like this:
VBA Code:
Public Sub PBL_SUB_Refresh_Pivotchart()
  ' Refresh Pivot chart in "Table 1" tab refreshes the data used to calculate the pivot chart
  ActiveWorkbook.RefreshAll

  On Error GoTo ErrHandler

  With ActiveSheet.PivotTables("PivotTable6").PivotFields("BRN Description")
    .PivotItems("0").Visible = False
    .PivotItems("(blank)").Visible = False
  End With

  Exit Sub
  
ErrHandler:
  Debug.Print Err.Number, Err.Description, Err.Source
  MsgBox "Error " & Err.Number & ": " & Err.Description & Chr(13) & "From: " & Err.Source & Chr(13) & "Function : " & "PBL_SUB_Refresh_Pivotchart"
End Sub

_________________________________________

Verifying if the data exists, it would look like this:

VBA Code:
Public Sub PBL_SUB_Refresh_Pivotchart()
  Dim f As Range
  ' Refresh Pivot chart in "Table 1" tab refreshes the data used to calculate the pivot chart
  ActiveWorkbook.RefreshAll
  With ActiveSheet.PivotTables("PivotTable6").PivotFields("BRN Description")
    Set f = Range(.DataRange.Address).Find("0", , xlValues, xlWhole)
    If Not f Is Nothing Then
      .PivotItems("0").Visible = False
    End If
    Set f = Range(.DataRange.Address).Find("(blank)", , xlValues, xlWhole)
    If Not f Is Nothing Then
      .PivotItems("(blank)").Visible = False
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,861
Members
449,052
Latest member
Fuddy_Duddy

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