简体   繁体   中英

Excel VBA UDF Comment “from the inside”

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

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