2

自动过滤器垂直排序数据,但我想水平过滤行。可以说我有下表:

1 2 2 1 2

BAEFF

BDEFF

CDEFF

我能做的是设置一个自动过滤器并只过滤第一列中包含“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

任何帮助是极大的赞赏!

4

1 回答 1

3

在合理费用的假设下,你不能。仅转换数据以使行获得列会容易得多,反之亦然。因此,我强烈建议与该选项Paste Special一起使用来转换数据。Transpose您甚至可以使用该TRANSPOSE()函数动态地执行此操作。

编辑:

现在我明白了 - 你想隐藏基于某个值的列。事实上,这可以使用宏来实现,所以我的第一个答案是不正确的 - 抱歉!周围有一些宏可以为您执行此操作。您可以将此类解决方案与自动过滤器结合使用。这是来自 OpenOffice.org 论坛的 king_026 的解决方案(稍微适应了表格结构 - 见下文):

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

所以,下表:

计算1

在 Col1 上的 Autofilter 和宏完成他的工作之后将如下所示:

计算

于 2012-05-18T13:45:06.840 回答