I tried reading filter criteria for a pivot table from data fields, such as startdate and enddate. Alternativly I would like to see the criteria beside the pivot table.
I found this macro, but it fails with
BASIC runtime error, property or method not found: getByName
What can I do? This is LibreOffice 24.2.4.2
Sub ApplyDateFilterToPivotTable()
Dim oSheet As Object
Dim oPivotTables As Object
Dim oPivotTable As Object
Dim oFilterDescriptor As Object
Dim startDate As String
Dim endDate As String
Dim oField As Object
Dim oFilterField As Object
Dim criteria(1) As Object
Dim criterion(1) As Object
' Get the active sheet
oSheet = ThisComponent.CurrentController.ActiveSheet
' Read the start and end dates from the criteria table
startDate = oSheet.getCellRangeByName("A1").String ' Adjust cell reference as needed
endDate = oSheet.getCellRangeByName("B1").String ' Adjust cell reference as needed
' Get the pivot table
oPivotTables = oSheet.DataPilotTables
oPivotTable = oPivotTables.getByIndex(0) ' Adjust if you have multiple pivot tables
' Get the filter field descriptor
oField = oPivotTable.getByName("Date") ' Adjust the field name as needed
oFilterField = oField.createFilterDescriptor(True)
' Set the filter criteria
criterion(0) = "Date"
criterion(1) = ">=" & startDate
criteria(0) = criterion
criterion(0) = "Date"
criterion(1) = "<=" & endDate
criteria(1) = criterion
oFilterField.Criteria = criteria
' Apply the filter
oField.setFilter(oFilterField)
' Refresh the pivot table
oPivotTable.refresh()
End Sub