【问题标题】:excel vba filtering and groupingexcel vba过滤和分组
【发布时间】:2016-03-15 19:06:52
【问题描述】:

我在 excel 中有这张表,我需要应用 AutoFilter

假设“合同”从 A1 列开始,“评论”从 B1 列开始。请参阅下面的示例:

合约评论 111 A 111 乙 111 c 222天 222 英尺 222 克 333天 333 英尺 第333章 444 乙 444天 444 厘米

我想过滤合同以显示合同组/合同组(合同组/合同组的示例是 111),其中该组/合同组中的任何合同都有注释 A。见下文:

注意:我还有一些与我的应用程序相关的其他数据,但在此示例中未显示。

合约评论 111 A 111 乙 111 c 333天 333 英尺 第333章

而且我还希望能够通过任何不包含“A”的评论进行过滤。结果应该如下:

合约评论 222天 222 英尺 222 克 444 乙 444天 444 厘米

【问题讨论】:

  • 你试过excel中的过滤选项吗?
  • 首先,欢迎来到 StackOverFlow。仅供参考,此站点并非旨在作为“请给我此代码”站点。但更多的是“帮助我找到代码中的错误”网站。话虽如此,我仍然在下面回答了你的问题。有用的是使用 Excel 中的宏记录器尝试建立您想要做的事情,然后将其带到这里,以便我们查看解决方案或我们可以在哪里为您提供帮助。

标签: vba excel filtering grouping


【解决方案1】:

这会将合同列自动筛选到合同集,该集中的任何合同都具有 A 注释。

要过滤掉值,您需要在数组中的文本之前添加一个 ,这似乎不起作用。因此,我在末尾添加了一个循环来手动遍历行,并查看 COntract 值是否在 ContainsA 数组中,如果它们隐藏行。

Sub SomeSub()

    Dim MyRange As Range, CommentRng As Range, ContractRng As Range
    Dim ContainA As Variant
    Dim i As Integer, k As Integer, r As Integer

    'Getting the Total Rows of the Sheet
    With ActiveSheet.UsedRange
        LastRow = .Rows(.Rows.Count).Row
    End With

    'Setting the USedRange of the Sheet
    Set MyRange = ActiveWorkbook.ActiveSheet.UsedRange

    'Setting the Comment and Contract Ranges
    Set CommentRng = ActiveWorkbook.ActiveSheet.Range(Cells(1, 2), Cells(LastRow, 2))
    Set ContractRng = ActiveWorkbook.ActiveSheet.Range(Cells(1, 1), Cells(LastRow, 1))

    'Filtering the Comment Column
    MyRange.AutoFilter Field:=2, Criteria1:="A"

    'Getting the number of visible cells in the Contract Range
    ' -1 to remove the First Row of the Comment headin
    ' -1 to set Array correctly, it Array(1) then the array( SomeValue, SomeOtherValue)
    TotalA = ContractRng.SpecialCells(xlCellTypeVisible).Count - 2

    'Setting the Array size
    ReDim ContainA(TotalA)
    'Setting the Array size
    ReDim NotContainA(TotalA)

    i = 0
    'For each visible cell in Column 2 "Comment"
    For Each cell In ContractRng.SpecialCells(xlCellTypeVisible)

        'If the Value is "Comment"
        If cell.Value = "Contract" Then

            'Do nothing

        Else
            'Set the Cell value into the Array
            ContainA(i) = cell
            'Increment i
            i = i + 1
        End If

    Next cell


    ActiveSheet.ShowAllData


    'Filitering the Column 1 Data, the Criteria1 needs to be a string Array
    'Join(Array1) joins the Array as a String
    'Split then splits the String as a String array
    MyRange.AutoFilter Field:=1, Criteria1:=Split(Join(ContainA)), Operator:=xlFilterValues



    ActiveSheet.ShowAllData

    'If you want to hide the groups which contain "A"
    'There is no "FILTEROUT" function
    'so this must be done manually
    For r = 1 To LastRow

        For k = 0 To TotalA

            If Cells(r, 1) = ContainA(k) Then

                Rows(r).EntireRow.Hidden = True

            End If

        Next k

    Next r

End Sub

【讨论】:

  • 我已经找到了像你这样的解决方案,但这不是我想要的。您的代码只会给出 2 个结果,但我希望能够过滤所有相同的合同,就像在示例 2 中一样。
  • @Pero 你想过滤“合同”列吗?
  • 是的,我猜它应该过滤列合同和 cmets。应该是这样的:如果合约 111 有一个注释“a”,则显示所有合约 111 及其 cmets。
猜你喜欢
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-07
  • 2015-04-10
  • 1970-01-01
  • 2012-05-14
相关资源
最近更新 更多