Help with For Next

oswalda

New Member
Joined
May 27, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I need a for next loop that cycles through the spreadsheet finding every row that has #N/A in column C and then deletes that row.

I' guessign it would look something like this


Dim testrange as range
Set testrange = C1:C300

For each row in testrange with #N/A in it

If Cell value is #N/A Delete entire Row

Next testrange

Tried all I know how to do for a few hours now - can't get anything to work.

thanks for your help
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
There are a few different ways to go. Here is a simple one.
VBA Code:
Sub DeleteNARows1()
    Dim WS As Worksheet
    Dim rng As Range
    Dim I As Long, LastRow As Long

    Set WS = ActiveSheet
    Set rng = WS.Range("C1:C300")

    LastRow = rng.Rows.Count
    For I = LastRow To 1 Step -1
        With rng.Cells(I, 1)
            If Application.IsNA(.Value) Then
                .EntireRow.Delete
            End If
        End With
    Next I
End Sub

If you are sure there are no empty cells in the range, then another possibility is
VBA Code:
Sub DeleteNARows2()
    Dim WS As Worksheet
    Dim rng As Range

    Set WS = ActiveSheet
    Set rng = WS.Range("C1:C300")

    rng.Replace What:="#N/A", Replacement:=vbNullString, LookAt:=xlWhole
    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 0
I expect this error because of problem in formula, so I think this could help you
you can try this
VBA Code:
Sub Delete_Error()
    On Error Resume Next
    Columns("C").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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