VBA code to delete entire rows if #N/A were found simultaneously in columns A, B & C

Nikit Strygin

New Member
Joined
Aug 19, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi!

I'm trying to make a VBA which would allow me to delete entire rows on the specific worksheets if #N/A were found simultaneously in columns A, B & C. For now I'm stucked with error "1004 object defined error". I would be very thankful for your help! Thank you in advance

VBA Code:
Sub CleanNA()
Dim ws As Variant
Dim x As Long
Dim columnsrange As Variant

ws = Array("Sheet 1", "Sheet 2", "Sheet 3")
Set columnsrange = Range("A:C")
    For x = 0 To UBound(ws)
        If ActiveWorkbook.Sheets(ws(x)).Range(columnsrange).Cells.Value = CVErr(xlErrNA) Then
            ActiveWorkbook.Sheets(ws(x)).Cells.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Clear
            ActiveWorkbook.Sheets(ws(x)).Cells.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Clear
        End If
    Next
Application.ScreenUpdating = True
MsgBox ("#N/A were successfully cleaned")

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi & welcome to MrExcel.

How about:

VBA Code:
Sub clearrows()
  Dim ws As Variant
  Dim x As Long
  
  ws = Array("Sheet 1", "Sheet 2", "Sheet 3")
  For x = 0 To UBound(ws)
    On Error Resume Next
    Sheets(ws(x)).UsedRange.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Clear
  Next
End Sub
 
Upvote 0
Hi & welcome to MrExcel.

How about:

VBA Code:
Sub clearrows()
  Dim ws As Variant
  Dim x As Long
 
  ws = Array("Sheet 1", "Sheet 2", "Sheet 3")
  For x = 0 To UBound(ws)
    On Error Resume Next
    Sheets(ws(x)).UsedRange.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Clear
  Next
End Sub
Thank you for your answer! But it seems to me that this is not what I meant. Let me try to explain it more precisely. I need a code which will look at row values in columns A, B and C and if the values in the row are simultaneously in 3 columns A,B and C equal to the "#N/A" then the entire row will be deleted, but if in the row there is text it won't be cleared
 
Upvote 0
Try this

VBA Code:
Sub clearrows()
  Dim ws As Variant
  Dim x As Long
  
  ws = Array("Sheet 1", "Sheet 2", "Sheet 3")
  For x = 0 To UBound(ws)
    With Sheets(ws(x))
      .Range("A:C").AutoFilter Field:=1, Criteria1:="#N/A"
      .Range("A:C").AutoFilter Field:=2, Criteria1:="#N/A"
      .Range("A:C").AutoFilter Field:=3, Criteria1:="#N/A"
      .AutoFilter.Range.Offset(1).Clear
      .ShowAllData
    End With
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,426
Members
448,961
Latest member
nzskater

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