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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
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
 

vikas_newports

Board Regular
Joined
May 26, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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
 

mohadin

Well-known Member
Joined
Mar 22, 2015
Messages
1,091
Office Version
  1. 2013
Platform
  1. Windows
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
 

vcoolio

Well-known Member
Joined
Jun 29, 2014
Messages
1,079

ADVERTISEMENT

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.
 
Solution

vcoolio

Well-known Member
Joined
Jun 29, 2014
Messages
1,079
You're welcome Vikas. I'm glad that we were able to help.

Cheerio,
vcoolio.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,174
Messages
5,640,589
Members
417,152
Latest member
DayTimeSeby

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
Top