Delete #N/A

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a sheet wherein I do a Vlookup. For the values that are not found, it generates the #N/A error.

I am trying to delete those rows which have the #N/A error, but it is not deleting


1663349041448.png


This is my code. I call the Del_Rows in my main code by using Call Del_Rows

<code>
Sub Del_Rows()
Dim wbexcel As Workbook
Dim ws As Worksheet
Dim LR As Long, i As Long

Set wbexcel = ThisWorkbook
Set ws = wbexcel.Sheets("TSS Fails")

LR = ws.Range("AU2:AU" & Rows.Count).End(xlUp).Row
For i = LR To Step - 1
If Cells("AU2" & i).Text = "#N/A" Then Rows(i).EntireRow.Delete
Next i

End Sub

</code>

What am I doing wrong?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:
VBA Code:
Sub Del_Rows()
    Dim wbexcel As Workbook, ws As Worksheet
    Set wbexcel = ThisWorkbook
    Set ws = wbexcel.Sheets("TSS Fails")
    ws.Columns("AU").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
End Sub
 
Upvote 0
Try:
VBA Code:
Sub Del_Rows()
    Dim wbexcel As Workbook, ws As Worksheet
    Set wbexcel = ThisWorkbook
    Set ws = wbexcel.Sheets("TSS Fails")
    ws.Columns("AU").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
End Sub
I tried that and get

1663351417202.png


1663351417202.png
 
Upvote 0
Here are comments explaining why your code didn't work (and corrections/and more explanations):

VBA Code:
Sub Del_Rows()

Dim wbexcel As Workbook
Dim ws As Worksheet

Dim LR As Long, i As Long
Set wbexcel = ThisWorkbook
Set ws = wbexcel.Sheets("TSS Fails")

'This doesn't work.
'LR = ws.Range("AU2:AU" & Rows.Count).End(xlUp).Row

'Use the following instead:
LR = ws.Cells(ws.Rows.Count, "AU").End(xlUp).Row

'This is incorrect syntax.
'Step comes AFTER the upper (well, in this case, lower bound).
'For i = LR To Step - 1
For i = LR To 1 Step -1
    'You have to put ws. before Cells("AU2"...) AND Rows(i) too.'
    'Not just Range(...)!  They all represent cells/ranges.
    'If Cells("AU2" & i).Text = "#N/A" Then Rows(i).EntireRow.Delete
  
    'And Cells("AU2"...) is also incorrect.
    'The syntax for Cells(...) is Cells(row#, column# or letter).
    If ws.Cells(i, "AU").Text = "#N/A" Then ws.Rows(i).EntireRow.Delete
Next i

End Sub

'And you don't need to set any objects to variables.
'So this sub could have been written like this to
'achieve the same result:
'Notice that there are STILL .'s in front of Cells, Rows, etc.
'With the WITH statement, it is saying (for line ##):
'LR = ThisWorkbook.Sheets("TSS Fails").Cells(ThisWorkbook.Sheets("TSS Fails").Rows.Count,"AU").End(xlup).Row
'It's like factoring in math.  Like how x^2+x = x(x+1).
Sub Del_Rows_2()

With ThisWorkbook.Sheets("TSS Fails")
    LR = .Cells(.Rows.Count, "AU").End(xlUp).Row '##
    For i = LR To 1 Step -1
        If .Cells(i, "AU").Text = "#N/A" Then .Rows(i).EntireRow.Delete
    Next i

End With

End Sub

I tried that and get
Is your sheet protected?
 
Upvote 0
Here are comments explaining why your code didn't work (and corrections/and more explanations):

VBA Code:
Sub Del_Rows()

Dim wbexcel As Workbook
Dim ws As Worksheet

Dim LR As Long, i As Long
Set wbexcel = ThisWorkbook
Set ws = wbexcel.Sheets("TSS Fails")

'This doesn't work.
'LR = ws.Range("AU2:AU" & Rows.Count).End(xlUp).Row

'Use the following instead:
LR = ws.Cells(ws.Rows.Count, "AU").End(xlUp).Row

'This is incorrect syntax.
'Step comes AFTER the upper (well, in this case, lower bound).
'For i = LR To Step - 1
For i = LR To 1 Step -1
    'You have to put ws. before Cells("AU2"...) AND Rows(i) too.'
    'Not just Range(...)!  They all represent cells/ranges.
    'If Cells("AU2" & i).Text = "#N/A" Then Rows(i).EntireRow.Delete
 
    'And Cells("AU2"...) is also incorrect.
    'The syntax for Cells(...) is Cells(row#, column# or letter).
    If ws.Cells(i, "AU").Text = "#N/A" Then ws.Rows(i).EntireRow.Delete
Next i

End Sub

'And you don't need to set any objects to variables.
'So this sub could have been written like this to
'achieve the same result:
'Notice that there are STILL .'s in front of Cells, Rows, etc.
'With the WITH statement, it is saying (for line ##):
'LR = ThisWorkbook.Sheets("TSS Fails").Cells(ThisWorkbook.Sheets("TSS Fails").Rows.Count,"AU").End(xlup).Row
'It's like factoring in math.  Like how x^2+x = x(x+1).
Sub Del_Rows_2()

With ThisWorkbook.Sheets("TSS Fails")
    LR = .Cells(.Rows.Count, "AU").End(xlUp).Row '##
    For i = LR To 1 Step -1
        If .Cells(i, "AU").Text = "#N/A" Then .Rows(i).EntireRow.Delete
    Next i

End With

End Sub


Is your sheet protected?
That worked although for a while I thought it was going through endless loop!
 
Upvote 0
@mumps method IS most definitely faster. But if your sheet is protected, it can prevent .SpecialCells from working.
 
Upvote 0
Mumps code should be
VBA Code:
ws.Columns("AU").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
 
Upvote 0
Solution
I'm glad it worked for you. I hope you will also (when you have time) take the time to read over the comments/correct to your code. It's fundamental concepts in VBA that you need to understand to prevent similar things from happening again in the future. (And that post was technically answering the question of this thread.)
 
Upvote 0

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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