UDF在调试模式下工作,但没有给单元的值
我第一次使用 VBA Excel UDF。 我的目标是创建类似于“SumIf”的东西,但它通过项目列表获得次要标准(写入单元格并用“|”分隔)。
我已经通过回调 sub 测试了这个 UDF,以完成逐步过程,在这里它工作正常(这是测试子: MsgBox SommaSeIncludo(Range("A:A"), Range( "B:B"), Range("B1"), Range("C:C"), "D1")
但是,我不知道为什么,如果我将公式放入它显示的单元格中只有一个“值”错误( =SommaSeIncludo(A:A;B:B;B1;C:C;D1)
)。 你有什么想法吗?
这是我的代码:
Function SommaSeIncludo(ByVal intSomma As Range, ByVal intCriteri1 As Range, ByVal criteri1 As Range, ByVal intCriteri2 As Range, ByVal listaCriteri2 As String) As String
Dim listaCriteri2Array As Variant
Dim numberOfListaCriteri2ArrayItems As Integer
Dim thisItemCriteria As String
Dim subTotal As Double
Dim total As Double
Dim thisSheet As Worksheet
Set thisSheet = ThisWorkbook.Sheets("MySheet")
total = 0
listaCriteri2Array = Split(thisSheet.Range(listaCriteri2).Value, "|")
numberOfListaCriteri2ArrayItems = UBound(listaCriteri2Array) - LBound(listaCriteri2Array)
For i = 0 To numberOfListaCriteri2ArrayItems
subTotal = 0
thisItemCriteria = listaCriteri2Array(i)
subTotal = WorksheetFunction.SumIfs(intSomma, intCriteri1, criteri1.Value, intCriteri2, thisItemCriteria)
total = subTotal + total
Next
SommaSeIncludo = total
End Function
I'm working with a VBA Excel UDF for the first time.
My target is to create something similar to "SumIf" but that gets secondary criteria by a list of item (wrote into a cell and separated by a "|").
I've already tested this UDF by a call back sub , to go through step by step process, and here it works fine (this is the test sub: MsgBox SommaSeIncludo(Range("A:A"), Range("B:B"), Range("B1"), Range("C:C"), "D1")
but, I don't know why, if I put the formula into the cell it shows only a "value" error ( =SommaSeIncludo(A:A;B:B;B1;C:C;D1)
).
Do you have some idea?
This is my code:
Function SommaSeIncludo(ByVal intSomma As Range, ByVal intCriteri1 As Range, ByVal criteri1 As Range, ByVal intCriteri2 As Range, ByVal listaCriteri2 As String) As String
Dim listaCriteri2Array As Variant
Dim numberOfListaCriteri2ArrayItems As Integer
Dim thisItemCriteria As String
Dim subTotal As Double
Dim total As Double
Dim thisSheet As Worksheet
Set thisSheet = ThisWorkbook.Sheets("MySheet")
total = 0
listaCriteri2Array = Split(thisSheet.Range(listaCriteri2).Value, "|")
numberOfListaCriteri2ArrayItems = UBound(listaCriteri2Array) - LBound(listaCriteri2Array)
For i = 0 To numberOfListaCriteri2ArrayItems
subTotal = 0
thisItemCriteria = listaCriteri2Array(i)
subTotal = WorksheetFunction.SumIfs(intSomma, intCriteri1, criteri1.Value, intCriteri2, thisItemCriteria)
total = subTotal + total
Next
SommaSeIncludo = total
End Function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论中的以下建议:
简短的答案是,您可以通过在调用函数时将 D1 转换为字符串来使其工作:
但可能更好的方法是将函数的第一行更改为范围,
如果您希望它引用D1 在指定的工作表中,我只知道 Split 语句中稍微冗长的构造,将范围转换为字符串,将其与工作表名称连接,然后转换回范围:
参见
也许最好不要将其绑定到不过,您始终可以将 MySheet!D1 放在函数调用中,并将 Split 行保留为
Following suggestions in the comments:
Short answer is you can get it work by making D1 into a string when you call the function:
But probably better to change it to a range by changing the first line of the function to
If you want it to reference D1 in a specified sheet, I only know the slightly long-winded construction in the Split statement to convert the range to a string, concatenate it with the sheet name, and convert back to a range:
See
Maybe it's better not to tie it to a particular sheet though, as you can always put MySheet!D1 in the function call, and leave the Split line as