gpt4 book ai didi

vba - 如何在返回 double 值的函数中显示单元格中的字符串

转载 作者:行者123 更新时间:2023-12-03 03:01:33 25 4
gpt4 key购买 nike

我有一个非常简单的 VBA 函数,它接受两个范围参数并使用两个范围的值返回一个 double 值。我在单个单元格的公式栏中使用它,但如果出现问题,我想返回字符串错误。有没有办法可以在单元格中不显示 #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

最佳答案

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

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

关于vba - 如何在返回 double 值的函数中显示单元格中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com