如何使用VBA存储和检索从AutoFilter操作返回的行号?例如,我使用@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
答案 0 :(得分:3)
使用Is it possible to fill an array with row numbers which match a certain criteria without looping?中的代码,您可以在没有AutoFilter
例如,此代码将返回在B2:B50000
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