-1

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.

Community
  • 1
  • 1
inadvisableguy
  • 107
  • 2
  • 12
  • 1
    Possible duplicate of [excel delete row if column contains value from to-remove-list](https://stackoverflow.com/questions/12393524/excel-delete-row-if-column-contains-value-from-to-remove-list) – ashleedawg Jun 06 '18 at 11:49

2 Answers2

1

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
JC Guidicelli
  • 1,296
  • 7
  • 16
1

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.

TBoulz
  • 339
  • 5
  • 20