我可以在OpenOffice Calc中创建水平自动过滤器

时间:2012-05-18 13:13:25

标签: macros openoffice-calc openoffice-basic

autofilter是垂直排序数据,但我希望水平过滤行。 可以说我有下表:

1 2 2 1 2

B A E F F

B D E F F

C D E F F

我能做的是设置自动过滤器并仅过滤第一列中包含“B”的行。我想要做的是只过滤包含“2”的行(在这种情况下,行是第二个,第三个,在这种情况下是最后一行)。

我找到了一些有关此事的信息。我找到的所有答案都包含一些宏来完成工作,但它们是为MS Excel编写的,与OpenOffice不兼容

例如,此宏应该过滤掉行,但在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

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

你不能在合理费用的假设下。转换数据以便行获取列更容易,反之亦然。因此,我强烈建议使用Paste SpecialTranspose选项一起转换数据。您甚至可以使用TRANSPOSE()函数动态执行此操作。

修改

现在我明白了 - 你想根据某个值隐藏列。这实际上可以使用宏,所以我的第一个答案是错误的 - 抱歉!周围有一些宏会为你做这件事。您可以将此类解决方案与自动过滤器结合使用。这是一个solution by king_026 from the OpenOffice.org forums(略微适应表结构 - 见下文):

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

所以,下表:

calc1

在Col1上的Autofilter之后以及在宏完成他的工作之后,

将会是这样的:

calc