簡體   English   中英

使用Excel用戶定義函數返回#value錯誤

[英]#value error returns with excel user defined function

我創建了用戶定義的函數來計算足球比賽結果。

我的根函數看起來:

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和matchResults ragne-everythink正常運行。

但是,當我嘗試從工作表中調用它時,在選定的單元格中出現了#VALUE錯誤。

單元格的#VALUE錯誤

但是在函數形式中有正確的結果:

函數返回的正確值

一個一直在嘗試調試返回值,並且我總是得到正確的值。 我只有問題與返回單元格中的錯誤。

有任何想法嗎 ?

問題是DisplayFormat對象不適用於UDF的

請參閱MSDN文章

通常的解決方案是評估條件格式條件,以確定哪個處於活動狀態。 例如, 請參閱cpearson.com

我通過代碼解決了問題:

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

代替:

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

此解決方案有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM