cells.find error handling

Rasberry

Board Regular
Joined
Aug 11, 2002
Messages
201
Office Version
  1. 365
I can't seem to get the correct error code handling for ending the sub when there are no more #N/A's to find. It just stops the code:

Sub RunMacro_Remove_NA()
Dim End_of_Monthly_Dates As Integer
Range("D9").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.End(xlDown).Select
End_of_Monthly_Dates = ActiveCell.Row
ActiveCell.Offset(0, 2).Select
EndRange = Selection
Range("E10").Select
For i = 1 To End_of_Monthly_Dates * 2
Cells.Find(What:="#N/A N/A", After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Delete Shift:=xlUp
Next i
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Just explain what you want to achieve.
The code from Post #1 needs cleaning anyway. Too many "Selects"
 
Upvote 0
Just explain what you want to achieve.
The code from Post #1 needs cleaning anyway. Too many "Selects"
I want to find "#N/A N/A" in a certain range of cells, and then delete the cell, and shift all cells below it up. Once it has cycled through the range, I want to exit the sub. What it does right now is once it has cleaned up all the "#N/A N/A" cells, and can't find anymore, it basically stops on the cells.find code line and the macro stops working rather than just exiting the sub.
 
Upvote 0
Re: "For i = 1 To End_of_Monthly_Dates * 2"
Why the *2?
Re: "certain range of cells" Be more specific.

Have you tried with AutoFilter?
 
Upvote 0
I think you are getting lost in the peripheral items. There are multiple ways I can define the range. I just need code to exit the loop once it cannot find the #N/A.
 
Upvote 0
Nothing serious. Just try to give you a better alternative.
 
Upvote 0
In keeping with your general programming strategy, perhaps something like this.
VBA Code:
Sub RunMacro_Remove_NA()
    Dim End_of_Monthly_Dates As Long
    Dim i As Long
    Dim rngFind As Range
    
    With ActiveSheet
        End_of_Monthly_Dates = .Range("D" & .Rows.Count).End(xlUp).Row
        .Range("E10").Select
        For i = 1 To End_of_Monthly_Dates * 2
            Set rngFind = Nothing
            On Error Resume Next
            Set rngFind = .Cells.Find(What:="#N/A N/A", After:=ActiveCell, _
            LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            On Error GoTo 0
            If Not rngFind Is Nothing Then
                rngFind.Delete Shift:=xlUp
            End If
        Next i
    End With
End Sub
 
Upvote 0
I agree with Jolivanes that this is a rather odd way of doing this. Typically you would use a Do Until Loop.
Since @rlv01's code doesn't actually exit the For loop when there is nothing more to find, here is a variation of that code.

Rich (BB code):
Sub RunMacro_Remove_NA()
    Dim End_of_Monthly_Dates As Long
    Dim i As Long
    Dim rngFind As Range
  
    With ActiveSheet
        End_of_Monthly_Dates = .Range("g" & .Rows.Count).End(xlUp).Row
        .Range("E10").Select
        For i = 1 To End_of_Monthly_Dates * 2
            Set rngFind = .Cells.Find(What:="#N/A N/A", After:=ActiveCell, _
                            LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            If rngFind Is Nothing Then
                Exit For
            End If
            rngFind.Delete Shift:=xlUp

        Next i
    End With
End Sub

PS: I split out "If rngFind Is Nothing Then Exit For" into multiple lines so that I could put a break point on the Exit For to see where it exited.
 
Last edited:
Upvote 0
Further to what @Alex Blakenburg said, a more standard approach would be a Do-Until loop. An example:
VBA Code:
Sub RunMacro_Remove_NA2()
    Dim rngFind As Range, SearchRange As Range
    
    Set SearchRange = ActiveSheet.UsedRange
    With SearchRange
        Do
            Set rngFind = .Cells.Find(What:="#N/A N/A", LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            If Not rngFind Is Nothing Then
                rngFind.Delete Shift:=xlUp
            End If
        Loop Until rngFind Is Nothing
    End With
End Sub

For the example, I am defining the search range as the used range of the ActiveSheet, but it can be defined in other ways depending on what cells you want to limit the search to.
 
Upvote 0
In keeping with your general programming strategy, perhaps something like this.
VBA Code:
Sub RunMacro_Remove_NA()
    Dim End_of_Monthly_Dates As Long
    Dim i As Long
    Dim rngFind As Range
   
    With ActiveSheet
        End_of_Monthly_Dates = .Range("D" & .Rows.Count).End(xlUp).Row
        .Range("E10").Select
        For i = 1 To End_of_Monthly_Dates * 2
            Set rngFind = Nothing
            On Error Resume Next
            Set rngFind = .Cells.Find(What:="#N/A N/A", After:=ActiveCell, _
            LookIn:=xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
            On Error GoTo 0
            If Not rngFind Is Nothing Then
                rngFind.Delete Shift:=xlUp
            End If
        Next i
    End With
End Sub
This works!!! As the other two models do as well. Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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