Filter N/A and Delete Rows

vikas_newports

Board Regular
Joined
May 26, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
Hello
I want to Delete the rows have #N/A after apply the ilter and criteria1 #N/A but it delete the one record without #N/A
here is my code
VBA Code:
Set rng4 = Range("B5:N" & last_row4)
        With rng4
                .AutoFilter
            .AutoFilter Field:=13, Criteria1:="#N/A"
            On Error Resume Next
            .SpecialCells(xlCellTypeVisible).EntireRow.Delete
            On Error GoTo 0
            .AutoFilter
        End With
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
May be other method will do
VBA Code:
Dim delrow As Range
    For i = 1 To last_row4
        If Range("N" & i).Text = "#N/A" Then
            If Not delrow Is Nothing Then
                Set delrow = Union(delrow, Range("N" & i))
            Else
                Set delrow = Range("N" & i)
            End If: End If
    Next
    delrow.EntireRow.Delete
 
Upvote 0
giving error on this line

delrow.EntireRow.Delete

May be other method will do
VBA Code:
Dim delrow As Range
    For i = 1 To last_row4
        If Range("N" & i).Text = "#N/A" Then
            If Not delrow Is Nothing Then
                Set delrow = Union(delrow, Range("N" & i))
            Else
                Set delrow = Range("N" & i)
            End If: End If
    Next
    delrow.EntireRow.Delete
 
Upvote 0
Trap an error if nothing to be deleted
VBA Code:
Dim delrow As Range
    For i = 1 To last_row4
        If Range("N" & i).Text = "#N/A" Then
            If Not delrow Is Nothing Then
                Set delrow = Union(delrow, Range("N" & i))
            Else
                Set delrow = Range("N" & i)
            End If: End If
    Next
    If delrow Is Nothing Then: MsgBox ("There Is Nothing To Delete"): Exit Sub
    delrow.EntireRow.Delete
 
Upvote 0
Hello Vikas,

Try using the AutoFilter as follows:-
VBA Code:
Sub Test()

        With Sheet1.Range("N5", Sheet1.Range("N" & Sheet1.Rows.Count).End(xlUp))
                .AutoFilter 1, "#N/A"
                .Offset(1).EntireRow.Delete
                .AutoFilter
        End With

End Sub

I'm assuming that Row5 is your headings row.
Sheet1 is a sheet code and you may have to change it to suit.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Solution
You're welcome Vikas. I'm glad that we were able to help.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,038
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top