【问题标题】:How can I display a string in a cell from a function returning a double如何从返回双精度的函数中显示单元格中的字符串
【发布时间】:2012-04-05 14:32:25
【问题描述】:

我有一个非常简单的 VBA 函数,它接受两个范围参数并使用两个范围的值返回一个双精度值。我在单个单元格的公式栏中使用它,但我想返回一个字符串错误,如果某些东西不太正确。有没有办法在单元格中不显示 #VALUE 的情况下显示它?

Public Function CPP(aPreviousPercentage As Range, aCurrentPercentage As Range) As Double

    If (aPreviousPercentage.Value > 0.2) Then
        CPP = "Invalid Starting Value"
        Exit Function
    End If

    CPP = aCurrentPercentage.Value - aPreviousPercentage.Value

End Function

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    可能最好的方法是将返回类型更改为变体而不是双精度。这样,如果您愿意,可以将其设置为字符串而不是数值。

    Public Function CPP(aPreviousPercentage As Range, aCurrentPercentage As Range) As Variant
    

    【讨论】:

    • 再次感谢您!我自己偶然发现了该解决方案,并即将发布答案。 :)
    猜你喜欢
    • 2010-09-24
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多