I have a user defined function that calls an external plugin as a wrapper and returns the manipulated result.

It works for the most part, but when the error from the external library comes back sometimes it reflects as #ERROR! BLAH BLAH BLAH etc.

For aesthetic reasons for users I'm trying to figure out a way to add a comment into the cell where the function is being called from and just return '-' or some indicative character.

I can't seem to find a way to find a reference to the calling cell.

I was hoping for something along the lines of:

If VarType(ret) = vbString And InStr("ret", "#ERROR!") > 0 Then
     <insert comment into Caller>
End If

Has anyone had the occasion to do this?

Thanks!

有帮助吗?

解决方案

Use Application.Caller:

Function WrapError(ret As Variant) As Variant
    Dim CatchErr As Boolean

    If VarType(ret) = vbString Then
        If InStr(ret, "#ERROR!") = 1 Then
            CatchErr = True
        End If
    End If

    If CatchErr Then
        WrapError = [#VALUE!]
        Application.Caller.AddComment ret
    Else
        WrapError = ret
        If Not Application.Caller.Comment Is Nothing Then
            Application.Caller.Comment.Delete
        End If
    End If
End Function
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top