简体   繁体   English

Excel VBA UDF评论“从内部”

[英]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! 它在大多数情况下都有效,但是当外部库中的错误返回时,它有时会反映为#ERROR! BLAH BLAH BLAH etc. BLAH BLAH BLAH等

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 : 使用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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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