ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Error handling (https://www.excelbanter.com/excel-programming/278670-error-handling.html)

John Pierce

Error handling
 
I have this simple program that captures a string and then jumps
to another sheet to look for that string. It works most of the but
causes an annoying error when "findThis" is not on the target sheet.
I have tried various "On Error GoTo . . ." with no success.

Sub FindThis()
'
Dim FindThis As String
FindThis = ActiveCell.Text
Sheets("Box Log").Select
Cells.Find(What:=FindThis, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
'
End Sub

Bill Barclift

Error handling
 
The easiest may be to use "On Error Resume Next":

On Error Resume Next
....your code here
if Err.Number 0 then
'log message
end if
On Error Goto 0

Bill Barclift


"John Pierce" wrote in message
om...
I have this simple program that captures a string and then jumps
to another sheet to look for that string. It works most of the but
causes an annoying error when "findThis" is not on the target sheet.
I have tried various "On Error GoTo . . ." with no success.

Sub FindThis()
'
Dim FindThis As String
FindThis = ActiveCell.Text
Sheets("Box Log").Select
Cells.Find(What:=FindThis, After:=ActiveCell, LookIn:=xlFormulas,

LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,

MatchCase:= _
False).Activate
'
End Sub




Tom Ogilvy

Error handling
 
Sub FindThis()
'
Dim rng as Range
Dim FindThis As String
FindThis = ActiveCell.Text
Sheets("Box Log").Select
set rng = Cells.Find(What:=FindThis, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:= _
False)
if not rng is nothing then
rng.Select
end if
'
End Sub

--
Regards,
Tom Ogilvy


John Pierce wrote in message
om...
I have this simple program that captures a string and then jumps
to another sheet to look for that string. It works most of the but
causes an annoying error when "findThis" is not on the target sheet.
I have tried various "On Error GoTo . . ." with no success.

Sub FindThis()
'
Dim FindThis As String
FindThis = ActiveCell.Text
Sheets("Box Log").Select
Cells.Find(What:=FindThis, After:=ActiveCell, LookIn:=xlFormulas,

LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,

MatchCase:= _
False).Activate
'
End Sub




Chip Pearson

Error handling
 
John,

The better way to work with Find is to write the code in the following
manner:

Dim FoundCell As Range
Set FoundCell = Cells.Find(....)
If FoundCell Is Nothing Then
' code for value not found
Else
' code for value found
End If

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"John Pierce" wrote in message
om...
I have this simple program that captures a string and then jumps
to another sheet to look for that string. It works most of the but
causes an annoying error when "findThis" is not on the target sheet.
I have tried various "On Error GoTo . . ." with no success.

Sub FindThis()
'
Dim FindThis As String
FindThis = ActiveCell.Text
Sheets("Box Log").Select
Cells.Find(What:=FindThis, After:=ActiveCell, LookIn:=xlFormulas,

LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,

MatchCase:= _
False).Activate
'
End Sub





All times are GMT +1. The time now is 10:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com