help with If statement

bradyj7

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

The code below which is part of a larger macro looks for the text 'Journey End Event' in column E and deletes all rows below it. However, sometimes this text in not present and an error occurs. My question is how can I modify it so that if 'Journey End Event' is not found then exit the journey function. The journey function is called inside another function, so if the text is not present then I want it to Exit sub.

Thank you

Code:
Journey()
Dim LastRow2 As Long, myCell As Range, myRange As Range
Dim myCell1 As Range

***********Other code************

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

*************other code***************

End sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try

Code:
Set found = Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False)
If found Is Nothing Then Exit Sub
found.Select
 
Upvote 0
Hi,

Do you mean do this
Code:
Journey()
Dim LastRow2 As Long, myCell As Range, myRange As Range
Dim myCell1 As Range
Dim found 

LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("E" & LastRow2)
    Set found = 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
        
    If found Is Nothing Then Exit Sub
    found.Select

End sub
 
Upvote 0
No, I meant like this

Code:
Dim LastRow2 As Long, myCell As Range, myRange As Range
Dim myCell1 As Range
Dim found

LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("E" & LastRow2)
    Set found = Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False)
        If found Is Nothing Then Exit Sub
        found.Select

        Set myCell = ActiveCell(2, 0)

        Set myRange = Range(myCell, myCell1)
        myRange.EntireRow.Delete
        
End Sub
 
Upvote 0
Hi,

I get a 'Type mismatch' error on this line
Code:
Set found = Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False)
Thank you
 
Upvote 0
Sory, try

Rich (BB code):
Dim LastRow2 As Long, myCell As Range, myRange As Range
Dim myCell1 As Range
Dim found As Range

LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("E" & LastRow2)
    Set found = Cells.Find(What:="Journey End Event", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False)
        If found Is Nothing Then Exit Sub
        found.Select

        Set myCell = ActiveCell(2, 0)

        Set myRange = Range(myCell, myCell1)
        myRange.EntireRow.Delete
        
End Sub
 
Upvote 0
Maybe

Code:
Dim LastRow2 As Long, myCell As Range, myRange As Range
Dim myCell1 As Range
Dim found As Range

LastRow2 = ActiveCell.SpecialCells(xlCellTypeLastCell).Row
    Set myCell1 = Range("E" & LastRow2)
    Set found = myCell1.Find(What:="Journey End Event", LookIn:=xlValues, LookAt:=xlPart)
        If found Is Nothing Then Exit Sub
        found.Select

        Set myCell = ActiveCell(2, 0)

        Set myRange = Range(myCell, myCell1)
        myRange.EntireRow.Delete
        
End Sub
 
Upvote 0
Hi,

Nope that is not working either. No error occurs but it is exiting the routine even though 'journey end event' is present in the column.

Thank you
 
Upvote 0
Try

Rich (BB code):
Set found = Columns("E").Find(What:="Journey End Event", LookIn:=xlValues, LookAt:=xlPart)
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,683
Members
452,937
Latest member
Bhg1984

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