Help writing If statement

bradyj7

Board Regular
Joined
Mar 2, 2011
Messages
106
Hi all,

The code below (part of a larger macro) searches for the text 'Journey End Event' in column E and deletes all the rows after it. However sometimes, the text 'Journey End Event' is not present and as expected and error occurs. My question is, how can I modify it so that If 'Journey End Event' is not found then Exit Sub?

All help appreciated

John


Code:
Sub journey()
..............
'Delete all rows after Journey end event
LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
Set myCell1 = Range("E" & LastRow2)
    Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
        Set myCell = ActiveCell(2, 0)
        Set myRange = Range(myCell, myCell1)
        myRange.EntireRow.Delete
...............
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Untested, as I can't see the rest of your code.

Try something like this, I can't guarentee it will work but may help you work it out:

Code:
lRow = Range("E1").End(xlDown).Row
Set sFound = Range("E1:E" & lRow).Find("Journey End Event")
If Not sFound Is Nothing Then Range(myCell, sFound).EntireRow.Delete
If sFound Is Nothing Then Exit Sub
 
Upvote 0
Code:
Sub journey()
'..............
'Delete all rows after Journey end event
Dim oFound As Object
Dim LastRow2 As Long
Dim myCell1 As Range
Dim myRange As Range
Dim myCell As Range
    LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("E" & LastRow2)
    Set oFound = Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False)
    If Not oFound Is Nothing Then
        Set myCell = oFound(2, 0)
        Set myRange = Range(oFound, myCell1)
        myRange.EntireRow.Delete
    End If
'...............
End Sub
 
Upvote 0
Another way of doing it (bit more messy than JamesW):

Code:
Sub journey()
..............
'Delete all rows after Journey end event
 
on error goto ErrorOut
 
LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
Set myCell1 = Range("E" & LastRow2)
    Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Activate
        Set myCell = ActiveCell(2, 0)
        Set myRange = Range(myCell, myCell1)
        myRange.EntireRow.Delete
on error goto 0
goto PastError
 
ErrorOut:
msgbox "'Journey End Event' could not be found"
exit sub
 
PastError:
................
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,633
Members
452,933
Latest member
patv

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