【问题标题】:Store AutoFilter Row Numbers using VBA使用 VBA 存储自动筛选行号
【发布时间】:2015-03-30 01:07:59
【问题描述】:

如何使用 VBA 存储和检索从自动筛选操作返回的行号?例如,我使用@brettdj 代码from this question(参见下面的代码)删除了 B 列下所有带有“X”的行。现在我需要用 X 存储行号(B4、B6、B9 - 参见下面的屏幕截图)因为我需要删除同一工作簿中其他工作表上的相同行。

Sub QuickCull()
    Dim ws As Worksheet
    Dim rng1 As Range
    Set ws = Sheets("Sheet1")
    Set rng1 = ws.Range(ws.[b2], ws.Cells(Rows.Count, "B").End(xlUp))
    Application.ScreenUpdating = False
    With ActiveSheet
            .AutoFilterMode = False
            rng1.AutoFilter Field:=1, Criteria1:="X"
            rng1.Offset(1, 0).EntireRow.Delete
            .AutoFilterMode = False
        End With
    Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 应用过滤器后,向后遍历rng1中的单元格:如果.RowHeight >0,则删除其他工作表上的该行。

标签: vba excel


【解决方案1】:

使用来自Is it possible to fill an array with row numbers which match a certain criteria without looping? 的代码,您可以在没有AutoFilter 的情况下快速返回这些行

例如,此代码将返回在 B2:B50000 中找到 X 的行范围

Sub GetEm()
Dim StrRng As String
StrRng = Join(Filter(Application.Transpose(Application.Evaluate("=IF(B2:B50000=""X"",""B""&ROW(B2:B50000),""X"")")), "X", False), ",")
If Len(StrRng) > 0 Then MsgBox Range(StrRng).EntireRow.Address & " could be deleted elsewhere"
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多