我是VBA的新手,并不熟悉编程,所以我希望在这里提供一些帮助。
这就是事情:
我有一张excel表,我需要扫描整行并删除C列和D列中有文字" NA"在同一行。
提前致谢。
答案 0 :(得分:1)
试试这个。你需要从最后开始循环:
Sub test()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("sh_test") 'edit your sheet name
Dim lastrow As Long
lastrow = ws.Range("C" & Rows.Count).End(xlUp).Row + 1 'maybe change letter of the column
For i = lastrow To 2 Step -1
If ws.Cells(i, 3).Value = "NA" And ws.Cells(i, 4).Value = "NA" Then
ws.Rows(i).EntireRow.Delete
End If
Next i
End Sub
答案 1 :(得分:1)
这是我之前使用过的东西。这将在C列和D列中应用过滤器,NA过滤器,然后删除这些行(不包括标题)。请注意,如果NA在BOTH列中,则只会删除该行。如果NA在C中但不在D中,那么它将不会删除该行。
'Applies filter to the columns. You will want to adjust your range.
Cells.Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$D$6").AutoFilter Field:=3, Criteria1:="NA"
ActiveSheet.Range("$A$1:$D$6").AutoFilter Field:=4, Criteria1:="NA"
'Removes the rows that are displayed, except the header
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
Application.DisplayAlerts = True
'Removes the filter
Cells.Select
Selection.AutoFilter
希望有所帮助。