简体   繁体   English

Excel VBA过滤和删除

[英]Excel VBA filter and delete

I searched the forum but wasnt able to find anything that really worked. 我搜索了论坛但却找不到真正有用的东西。 I have a simple sheet where i want to filter column AZ for anything labeled "LOW" or "TBD" and delete only those. 我有一个简单的工作表,我想过滤任何标记为“低”或“TBD”的列AZ,并删除那些。 Then remove the filter. 然后删除过滤器。

I was running the below code but since it deleted each one in line it would miss some as the i would just keep going. 我正在运行下面的代码,但由于它删除了每一个代码,它会错过一些,因为我会继续前进。

For i = 2 To access.UsedRange.Rows.Count

If access.Cells(i, 11) = "LOW" Or "TBD" Then
Rows(i).delete
Exit For
End If

Next i
If access.Cells(i, 11) = "LOW" Or  access.Cells(i, 11) = "TBD" Then

or if you want to be a bit more fancier: 或者如果你想更有点发烧友:

With access.Cells(i, 11)
    If .Value2 = "LOW" Or .Value2 = "TBD" Then Rows(i).Delete
End With

Whenever you are deleting rows, this is the best practice: 无论何时删除行,这都是最佳做法:

For i = access.UsedRange.Rows.Count to 2 Step -1

If it is a bit slow, consider adding the rows to a range with Union() and deleting the whole range. 如果它有点慢,可以考虑使用Union()将行添加到范围并删除整个范围。 The Union() should be the fastest solution I guess - How do I ignore the first row in an Excel macro that deletes all rows based on a criteria? Union()应该是我猜的最快的解决方案 - 如何忽略Excel宏中根据条件删除所有行的第一行?

When ever you are deleting records in a range in Excel and you referencing the rows using the row number you should start at the bottom and work your way to the top. 如果您要删除Excel中某个范围内的记录,并使用行号引用行,则应从底部开始,然后按自己的方式进行操作。 The reason is because when you're examine row 5 and you remove it, row 6 now becomes row 5 but your loop counter increments to row 7 - thus you never examine row 6. 原因是当你检查第5行并删除它时,第6行现在变为第5行,但是你的循环计数器增加到第7行 - 因此你永远不会检查第6行。

I would suggest: 我会建议:

Dim LastRow as Long

LastRow = access.UsedRange.Rows.Count
For i = LastRow To 2 Step - 1
    If access.Cells(i, 11) = "LOW" Or "TBD" Then
        Rows(i).delete
    End If
Next i

Toy can try this way. 玩具可以这样试试。 And why you add 11 for column index. 为什么你为列索引添加11。 AZ is index 50 AZ是指数50

Sub test2()


' Active workbook
Dim wb As Workbook
Set wb = ThisWorkbook
Dim i As Long


'*******************************************
'Adapt this vars


'define your sheets
Dim ws_1 As Worksheet
Set ws_1 = wb.Sheets("Feuil1")

'definie the last Rows
Dim lastRow_ws1 As Long

lastRow_ws1 = ws_1.Range("AZ" & Rows.Count).End(xlUp).Row
'*******************************************


For i = lastRow_ws1 To 2 Step -1

    Dim keySearch As String
    keySearch = ws_1.Cells(i, 50).Value

    If keySearch = "LOW" Or keySearch = "TBD" Then
    ws_1.Rows(i).EntireRow.Delete
    End If


Next i

End Sub

Here is a version that uses AutoFilter instead of running through the rows 1-by-1. 这是一个使用AutoFilter而不是AutoFilter遍历行的版本。 As a warning, this will clear any filters that you have on the data. 作为警告,这清除您对数据的任何过滤器。 (Unless you have macros to save/restore filters) (除非你有保存/恢复过滤器的宏)

Const FilterColumn = 52 ' Column AZ

'Clear Filters
access.AutoFilterMode = False
'Filter for data
access.UsedRange.AutoFilter FilterColumn, "LOW", xlOr, "TBD"
'If Data exists
If access.Cells(access.Rows.Count, FilterColumn).End(xlUp).Row > 1 Then
    'Delete visible rows
    access.Range(access.Cells(2, FilterColumn), access.Cells(access.Rows.Count, FilterColumn)).EntireRow.Delete
End If
'Clear Filters
access.AutoFilterMode = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM