我正在尝试做的是如果出现错误,则将重置某些值。就像是:

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

但是,如果出现错误,我如何强制其返回For

最佳答案

可以使用IsError代替ErrHanlding在Error上设置r的值:

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

关于vba - 出错时给变量赋值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37471571/

10-13 03:15