UDF数组导致#Value [重复]
Posted
技术标签:
【中文标题】UDF数组导致#Value [重复]【英文标题】:UDF Array resulting in #Value [duplicate] 【发布时间】:2020-06-08 15:19:15 【问题描述】:我有一个 UDF,详情如下。当我通过在 Sub 中调用公式并使用 msgbox 返回结果来逐步执行公式时,我最终得到了正确的结果。所以它似乎工作正常。但是,当我在工作表的单元格中用作 UDF 时,我会得到 #VALUE。我错过了什么?
Function ToolStatus(PartNumber As String, Model As String, Number As Integer) As String
Dim SearchSheet As Worksheet
Dim PN As Long
Dim MdlCol As Long
Dim Mdl As String
Dim Result As Long
Dim SearchArray As Variant
Dim PartCount As Long
Dim i As Long
Application.Volatile True
ToolStatus = ""
PartCount = WorksheetFunction.CountA(Sheet2.Range("A:A"))
Select Case True
Case Number < PartCount And Model = "1A"
Set SearchSheet = Sheet2
PN = 3
MdlCol = 5
Mdl = "1A"
Result = 20
Case Number < PartCount And Model = "1B"
Set SearchSheet = Sheet2
PN = 3
MdlCol = 6
Mdl = "1B"
Result = 20
Case Number < PartCount And Model = "1C"
Set SearchSheet = Sheet2
PN = 3
MdlCol = 7
Mdl = "1C"
Result = 20
Case Number >= PartCount And Model = "1A"
Set SearchSheet = Sheet3
PN = 2
MdlCol = 18
Mdl = "-1A"
Result = 5
Case Number >= PartCount And Model = "1B"
Set SearchSheet = Sheet3
PN = 2
MdlCol = 19
Mdl = "-1B"
Result = 5
Case Number >= PartCount And Model = "1C"
Set SearchSheet = Sheet3
PN = 2
MdlCol = 20
Mdl = "-1C"
Result = 5
End Select
SearchArray = SearchSheet.Range("A1").CurrentRegion
For i = LBound(SearchArray, 1) To UBound(SearchArray, 1)
If SearchArray(i, PN) = PartNumber And SearchArray(i, MdlCol) = Mdl Then
ToolStatus = SearchArray(i, Result)
Exit For
End If
Next i
End Function
【问题讨论】:
请您添加所有重要的第一行。 抱歉,错过了。我已经更新了。 ***.com/questions/30065057/…... 我认为CurrentRegion
在 UDF 中效果不佳。
如果 searchSheet 什么都没有,就会显示错误。在 If 条件中设置 For-Next 循环:If Not searchSheet Is Nothing Then ....
【参考方案1】:
正如@BigBen 指出的那样,.CurrentRegion
是问题所在。我将SearchArray = SearchSheet.Range("A1").CurrentRegion
替换为SearchArray = SearchSheet.Range("A1:CD2000").Value
只是为了进行测试,它确实有效。所以我需要弄清楚如何调整范围,但现在我知道问题是CurretnRegion
。
【讨论】:
以上是关于UDF数组导致#Value [重复]的主要内容,如果未能解决你的问题,请参考以下文章
在 PySpark 中使用 pandas_udf 时无法填充数组