VBA: If Cell contains #N/A delete selected range

NessPJ

Active Member
Joined
May 10, 2011
Messages
420
Office Version
  1. 365
Hi guys,

I've been fiddling around with a routine where i like to look for #N/A values in a selected range and if it is found delete a range of Cells on that row.

So far i've tried searching for String and Error values but i'm probably doing something wrong:

Code:
LastRowATImpShtResult3 = Sheets(ATTargetSht).Range("BZ" & "65536").End(xlUp).Row
    
    Dim XResult As Error
    
   n = 0


   For n = LastRowATImpShtResult3 To 3 Step -1
        XResult = Range("CB" & n).Err
        If XResult = xlErrNA Then
        Range("BW" & n & ":CF" & n).Delete Shift:=xlUp
        End If
    Next n

Can anyone help me with this please?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this out:

Code:
With Sheets(ATTargetSht)
    LastRowATImpShtResult3 = .Cells(.Rows.Count, "BZ").End(xlUp).Row
    If LastRowATImpShtResult3 > 2 Then
        For n = LastRowATImpShtResult3 To 3 Step -1
            If IsError(.Range("CB" & n).Value) Then
                If .Range("CB" & n).Value = CVErr(xlErrNA) Then
                    .Range("BW" & n & ":CF" & n).Delete Shift:=xlUp
                End If
            End If
        Next n
    End If
End With

This presumes the deletion should happen on the same sheet as the error cells are found.
 
Upvote 0
Hello,

You could test following ...

Code:
If(IsError(Range("CB" & n).Value) Then

Hope this will help
 
Upvote 0
Hello,

You could test following ...

Code:
If(IsError(Range("CB" & n).Value) Then

Hope this will help

That will work of course if the range to be searched is on the activesheet and it doesnt matter if the error is anything other than NA as per OP request.
 
Upvote 0
Thanks for the help guys!

Both solutions work although the VB Editor told me this had a Syntax error until i changed it to:

Code:
If IsError(Range("CB" & n).Value) Then
 
Last edited:
Upvote 0
Put a sheet qualifier on your range. You could cause yourself problems depending on when you run the macro.
 
Upvote 0

Forum statistics

Threads
1,215,528
Messages
6,125,342
Members
449,218
Latest member
Excel Master

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