I'm new to VBA and not very adept with the programming so I'm hoping for some help here.
Here's the thing:
I have an excel sheet where I need to scan the entire rows and delete rows where Column C and Column D have the text "NA" in the same row.
Thanks in advance.
Try this. You need to start loop from the end :
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
Here is something I've used before. This applies filters, filters for NA in both column C and D, then removes those rows (excluding the header). Note that this will only remove the row if NA is in BOTH columns. If NA is in C but not in D then it will not remove the row.
'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
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.