Search code examples
macrosopenoffice-calcopenoffice-basic

Can I create horizontal autofilter in OpenOffice Calc


The autofilter is sorting data vertically, but I want to filter rows horizontally. Lets say that I have the following table:

1 2 2 1 2

B A E F F

B D E F F

C D E F F

What I can do is to set an autofilter and filter only the rows containing "B" in the first column. What I would like to do is to filter only the rows that contain "2" (in this case the rows are second, third and the last in this case).

I have found some information regarding this matter. All of the answers I found are containing some macros to get the job done, but they were written for MS Excel, and are not compatible with OpenOffice

For example, this macros should get the rows filtered, but is not working in OpenOffice Calc:

Option Explicit

Sub horizontal_filter()
'Erik Van Geit
'060910

Dim LC As Integer           'Last Column
Dim R As Long
Dim i As Integer
Dim FilterValue As String

Const FilterColumn = 1      '1 is most logical value but you may change this

R = ActiveCell.Row
LC = Cells(R, Columns.Count).End(xlToLeft).Column

FilterValue = Cells(R, FilterColumn)

Application.ScreenUpdating = False

'to filter starting after FilterColumn
For i = FilterColumn + 1 To LC
'to filter all columns even before the filtercolumn
'For i = 1 To LC
    If i <> FilterColumn Then
    Columns(i).Hidden = Cells(R, i) <> FilterValue
    End If
Next i

Application.ScreenUpdating = True

End Sub

Any help is greatly appreciated!


Solution

  • You can't, under the assumption of reasonable expense. It's much easier just to transform your data so that rows get columns and vice versa. So, i would strongly recommend transforming the data using Paste Special together with the Transpose option. You could even do this dynamically by using the TRANSPOSE() function.

    EDIT:

    Now i got it - you want to hide columns based on a certain value. This is possible using a macro in fact, so my first answer was incorrect - sorry for that! There are some macros around that will do this for you. You can combine such a solution with an auto filter. Here's a solution by king_026 from the OpenOffice.org forums (slightly adapted to table structure - see below):

    REM  *****  BASIC  *****
    sub hide
       rem ----------------------------------------------------------------------
       rem define variables
       dim document   as object
       dim dispatcher as object
       rem ----------------------------------------------------------------------
       rem get access to the document
       document   = ThisComponent.CurrentController.Frame
       dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    
       rem get the current column
       nCol = ThisComponent.CurrentSelection.CellAddress.Column
    
       rem set the properties for moving right
       dim args2(1) as new com.sun.star.beans.PropertyValue
       args2(0).Name = "By"
       args2(0).Value = 1
       args2(1).Name = "Sel"
       args2(1).Value = false
    
       rem make thecurrent column counter
       dim cCol as integer
       CCol = 0
    
       rem goto the first column
       dim args1(0) as new com.sun.star.beans.PropertyValue
       args1(0).Name = "ToPoint"
       args1(0).Value = "$A$2"
    
       dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
    
       rem loop until you get back to the selected cell
        Do Until cCol > nCol
    
        rem hide if the cell value is 1
            if ThisComponent.CurrentSelection.string <> "" and ThisComponent.CurrentSelection.value = 1 then
    
                rem ----------------------------------------------------------------------
                dispatcher.executeDispatch(document, ".uno:HideColumn", "", 0, Array())
    
            End if
    
            rem goto the right nad increment the column counter
            dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args2())
            cCol = cCol + 1
    
        Loop
    
    End sub
    

    So, the following table:

    calc1

    will look like this after Autofilter on Col1 and after the macro did his work:

    calc