简体   繁体   中英

VBA Pivot Table Filter Run-time error '1004': Application-defined or object-defined error

I am trying to write a VBA script to update a pivot table's filters based on the value in two cells but I keep getting a 1004 run-time error. I have tried various approaches based on answers to other questions but I still can't figure out what the problem is.

Note: I have read that the error sometimes occurs because pivot tables require at least one visible value, but this error occurs even if I set Visible = True

VBA Code:

Private Sub PageItemFilter()
Dim pvtF As PivotField
Dim pvtI As PivotItem
Dim startDate As Date
Dim endDate As Date
Dim filterDate As Date

startDate = Range("start_date").Value
endDate = Range("end_date").Value

Set pvtF = Worksheets("selection").PivotTables("PivotTable1").PivotFields("[tbl_Main].[TransactionDate].[TransactionDate]")
pvtF.ClearAllFilters
For Each pvtI In pvtF.PivotItems
    filterDate = DateValue(Mid(pvtI.Name, 24, 10))
    If filterDate >= startDate And filterDate <= endDate Then
        Debug.Print (pvtI.Name)
        Debug.Print (TypeName(pvtI))
        Debug.Print (pvtI.Visible)
        pvtI.Visible = True
    Else
        pvtI.Visible = False
    End If
Next pvtI

End Sub

Output (first item):

[tbl_Main].[TransactionDate].&[2019-08-05T00:00:00]
PivotItem
True

Error: Run-time error '1004': Application-defined or object-defined error

try add condition to check does pivot item is visible like

 ..... code
 if not pvtI.Visible = True then pvtI.Visible = True 
 Else
     if not pvtI.Visible = false then pvtI.Visible = false 
 End If
 Next pvtI
 End Sub

Thank you to @TimWilliams for directing me to this solution . The short answer is that what I was trying to do is not possible with OLAP PivotTables. I implemented a version of the suggested solution and it works like a charm. I have included my solution code below for anyone stumbling upon this question.

Sub PageItemFilter()
Dim pvtF As PivotField
Dim pvtI As PivotItem
Dim startDate As Date
Dim endDate As Date
Dim filterDate As Date
Dim visibleArray() As String
Dim isEmpty As Boolean

ReDim visibleArray(1 To 1) As String

startDate = Range("start_date").Value
endDate = Range("end_date").Value
isEmpty = True

Worksheets("selection").Activate
Set pvtF = Worksheets("selection").PivotTables("PivotTable1").PivotFields("[tbl_Main].[TransactionDate].[TransactionDate]")
pvtF.ClearAllFilters

If startDate > endDate Then
    MsgBox "The start date is after the end date. Cannot update filters"
    Exit Sub
End If

For Each pvtI In pvtF.PivotItems
    filterDate = DateValue(Mid(pvtI.Name, 35, 10))
    If filterDate >= startDate And filterDate <= endDate Then
        isEmpty = False
        visibleArray(UBound(visibleArray)) = pvtI.Name
        If filterDate >= endDate Then
            Exit For
        Else
            ReDim Preserve visibleArray(1 To UBound(visibleArray) + 1) As String
        End If
    End If
Next pvtI

If isEmpty Then
    MsgBox "No data for the dates selected. Cannot update filters"
    Exit Sub
Else
    ActiveSheet.PivotTables("PivotTable1").PivotFields("[tbl_Main].[TransactionDate].[TransactionDate]").VisibleItemsList = visibleArray
End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM