简体   繁体   中英

Assign value to a variable upon error

What I am trying to do is that certain value gets reset if there has been an error. Something like:

Dim r As Integer
Dim feeder As String
Dim origen As String

On Error GoTo errhandler:
For j=1 to 100
   r = Evaluate("=MATCH(1, (C:C=""" & feeder & """) * (D:D=""" & origen & """),0)")
Next j

Exit Sub

errhandler:
r = 0
End Sub

But how do I force it to go back to the For if there is an error?

Instead of ErrHanlding you could use IsError to set the value of r on Error:

Sub t1()
  Dim feeder As String
  Dim origen As String
  For j = 1 To 100
   Dim r As Variant
   If IsError(Evaluate("=MATCH(1, (C:C=""" & feeder & """) * (D:D=""" & origen & """),0)")) Then
    r = 0
   Else
    r = Evaluate("=MATCH(1, (C:C=""" & feeder & """) * (D:D=""" & origen & """),0)")
   End If
  Debug.Print r ' Just for you to check
 Next

End Sub

Just use Resume Next statement

Dim r As Integer
Dim feeder As String
Dim origen As String

On Error GoTo errhandler:
For j=1 to 100
   r = Evaluate("=MATCH(1, (C:C=""" & feeder & """) * (D:D=""" & origen & """),0)")
Next j

Exit Sub

errhandler:
r = 0
resume Next

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM