View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
keiji kounoike keiji kounoike is offline
external usenet poster
 
Posts: 199
Default Macro Error Handling

I think there is no need to handle errors in your case as Dave said and
i think Dave's is the right way to go. I think the cause of a fail to
handle error second time is you can not allocate errors to another
handler within a active handler. if you want to use "On Error GoTo"
statement, something like this might work. but i wouldn't use this code.

Sub MacroTestErrorHandling()
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo BillingDate
Cells.Find(What:="Account Number", After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, _
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("A2").Select
ActiveSheet.Paste
Re1:
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo PaymentsReceived
Cells.Find(What:="Billing Date", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, _
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("B2").Select
ActiveSheet.Paste
Re2:
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo CorporateName
Cells.Find(What:="Payment Received", After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("H2").Select
ActiveSheet.Paste
Re3:
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False

Exit Sub

BillingDate:
On Error GoTo 0 '<<==No need but just for testing
Resume Re1

PaymentsReceived:
On Error GoTo 0 '<<==No need but just for testing
Resume Re2

CorporateName:
On Error GoTo 0 '<<==No need but just for testing
Resume Re3

End Sub

Keiji

Greg wrote:
Dave and OssieMac,
Thank you for your response.
Dave, I am reviewing what you suggested in your response.

OssieMac, I am pasting a small portion of the recorded Macro that I have
modified. I still do not understand why I take the On Error branch once but
not the second time.

Sub MacroTestErrorHandling()
'
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo BillingDate
Cells.Find(What:="Account Number", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
_
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("A2").Select
ActiveSheet.Paste
'
BillingDate:
On Error GoTo 0
Err.Clear
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo PaymentsReceived
Cells.Find(What:="Billing Date", After:=ActiveCell, LookIn:=xlFormulas,
_
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
_
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("B2").Select
ActiveSheet.Paste
'
PaymentsReceived:
On Error GoTo 0
Err.Clear
Sheets("Source").Select
Range("A1").Select
Application.CutCopyMode = False
On Error GoTo CorporateName
Cells.Find(What:="Payment Received", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Copy
Sheets("Target").Select
Range("H2").Select
ActiveSheet.Paste
'
CorporateName:








End Sub

Thank you both for your replies. OssieMac any suggestions would be
appreciatted.
Greg


"Greg" wrote in message
...
I have recorded a Macro and I am modifying it(easiest way for me to

learn).
However I have run into a situation I can not find a solution in any
documentation.

Basically I have series of "cells.find" in sheet 1 of a workbook followed

by
a copy and paste to sheet 2 in the workbook.

All is well if I find all the information I am trying to find with the
cells.find.

Before each "cells.find" I have inserted a statement "On Error goto

Label1"
or Label2, etc.

Basically I am skipping the copy and paste and looking for the next
information I need with the next "cells.find" .

The first time I perform a "cells.find" and do not find the required
information I branch correctly to the next part of the macro that does
another "cells.find".

If I encounter another error condition the macro does not take the branch

to
On Error GoTo Label20. It generates a run time error (91).

I have added "err.Clear" and "On Error GoTo 0" but I still get a runtime
error on the second failure of the "cells.find".

I can not get the Macro to reset the "On Error GoTo Labeln".

Does anyone have any suggestions on how to remedy this situation. I

believe
there may be some Pure VB solutions but I don't think I am ready to tackle
them yet when I can't even get this "dumb" macro to work.

Thank you in advance for your assistance.

Greg