I’m trying to develop a Basic macro that can create an XSheetFilterDescriptor
to filter the rows of a Calc sheet to display rows for which the string values in Column A match one of several string values in the same way as an AutoFilter does when only a few individual values are selected. However, no matter what I do, the result seems to ignore all but the first specified value. Here is my code:
Dim oSheet As Object
Dim oFilter As Object
Dim filterValues() As Variant
Dim filterFields(0) As new com.sun.star.sheet.TableFilterField3
Dim i As Integer
Dim op as Long
' Get the active sheet
oSheet = ThisComponent.getCurrentController.getActiveSheet()
' Define the filter values to be matched
filterValues = Array("Apple", "Banana", "Cherry")
Dim vals(UBound(filterValues)) as new com.sun.star.sheet.FilterFieldValue
' Create a new empty filter descriptor
oFilter = oSheet.CreateFilterDescriptor(True)
' Populate filter conditions
For i = 0 To UBound(filterValues)
filterFields(0).Connection = com.sun.star.sheet.FilterConnection.OR
filterFields(0).Field = 0 ' Column A
filterFields(0).Operator = com.sun.star.sheet.FilterOperator2.EQUAL
Dim val As New com.sun.star.sheet.FilterFieldValue
val.IsNumeric = False
val.StringValue = filterValues(i)
val.FilterType = com.sun.star.sheet.FilterFieldType.STRING
vals(i) = val
Next i
filterFields(0).Values = vals
' Apply the filter
oFilter.setFilterFields3(filterFields)
oSheet.filter(oFilter)
When run, it displays only rows holding “Apple” in column A, the other rows are hidden, even if they contain one of the other specified values.
Any assistance would be appreciated.