Search code examples
excelvbaruntime-error

Code Line Range.PivotCell is causing an error (1004) in Excel 2013 but not Excel 2017


The code below works fine on Excel 2016 but is giving me an Object Defined 1004 error on the 'If (Not Rng.Pivotcell'

Microsoft Script Runtime is active as reference. That had fixed a 1004 error for me before but with this one I am at a loss.

''Returns true if the column belongs to a pivot or not
Public Function ColumnBelongsToAPivot(ByVal ColumnLetter As String, ByVal RowIndex As Long, ByVal UseSheet As Worksheet) As Boolean
ColumnBelongsToAPivot = False

On Error GoTo Bye:

Dim X1 As Long
Dim ColumnRange1 As Range
Dim Rng As Range

Set Rng = UseSheet.Range(ColumnLetter & RowIndex)
If (Not Rng.PivotCell Is Nothing) Then
    ColumnBelongsToAPivot = True
Else
    ColumnBelongsToAPivot = False
End If

Bye:
End Function

enter image description here


Solution

  • Calling Rng.PivotCell when Rng is not in a pivot table will trigger a run-time error: that error should be caught by your On Error GoTo Bye, so if you're still getting an error then it's possible you have your VBA error handling option set to "Break on all errors".