【问题标题】:#value error returns with excel user defined function#value 错误返回与 excel 用户定义的函数
【发布时间】:2018-11-27 20:00:14
【问题描述】:

我创建了用户定义的函数来计算足球比赛结果。

我的根函数看起来是:

Function calculatePoints(personTypes As Range, matchesResults As Range) As Integer
    calculatePoints = getAllPersonPoints(personTypes, matchesResults)
End Function

getAllPersonPoints 函数:

Private Function getAllPersonPoints(personTypes As Range, matchesResults 
AsRange) As Integer
    Dim x As Long
    Dim y As Long
    Dim isTheSurest As Boolean
    getAllPersonPoints = 0

    For x = 1 To personTypes.Rows.Count
        For y = 1 To personTypes.Columns.Count
            isTheSurest = isTheSurestResult(personTypes.Cells(x, 
y).DisplayFormat.Interior.PatternColorIndex)
            getAllPersonPoints = getAllPersonPoints + 
getPoints(matchesResults.Cells(x, y).Value, personTypes.Cells(x, y).Value, 
isTheSurest)
        Next y
    Next x
End Function

当我尝试通过手动设置参数来调用此函数时:personTypes range 和 matchesResults ragne -everythink 工作正常。

但是当我试图从工作表中调用它时,我在选定的单元格中出现#VALUE 错误。

但在函数形式中,结果是正确的:

A 一直在尝试调试返回值,但我总是得到正确的值。我的问题只是返回单元格中的错误。

有什么想法吗?

【问题讨论】:

  • 为什么你有一个单独的函数calculatePoints 除了调用另一个函数之外什么都不做?

标签: excel vba


【解决方案1】:

问题是 DisplayFormat 对象不适用于 UDF

MSDN article

通常的解决方案是评估条件格式条件以确定哪个是活动的。例如see cpearson.com

【讨论】:

    【解决方案2】:

    我通过代码解决了问题:

    personTypes.Cells(x, y).Interior.ColorIndex
    

    代替:

    personTypes.Cells(x,y).DisplayFormat.Interior.PatternColorIndex
    

    此解决方案有效。

    【讨论】:

    • 您知道它不会检测条件格式颜色,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2016-06-10
    相关资源
    最近更新 更多