UDF在调试模式下工作,但没有给单元的值

发布于 2025-01-17 18:07:52 字数 1348 浏览 2 评论 0原文

我第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2025-01-24 18:07:52

评论中的以下建议:

简短的答案是,您可以通过在调用函数时将 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 Range) As Long

如果您希望它引用D1 在指定的工作表中,我只知道 Split 语句中稍微冗长的构造,将范围转换为字符串,将其与工作表名称连接,然后转换回范围:

 listaCriteri2Array = Split(Range("MySheet" & "!" & listaCriteri2.Address).Value, "|")

参见

也许最好不要将其绑定到不过,您始终可以将 MySheet!D1 放在函数调用中,并将 Split 行保留为

listaCriteri2Array = Split(listaCriteri2.Value, "|")

Following suggestions in the comments:

Short answer is you can get it work by making D1 into a string when you call the function:

=SommaSeIncludo(A:A;B:B;B1;C:C;"D1")

But probably better to change it to a range by changing the first line of the function to

Function SommaSeIncludo(ByVal intSomma As Range, ByVal intCriteri1 As Range, ByVal criteri1 As Range, ByVal intCriteri2 As Range, ByVal listaCriteri2 As Range) As Long

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:

 listaCriteri2Array = Split(Range("MySheet" & "!" & listaCriteri2.Address).Value, "|")

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

listaCriteri2Array = Split(listaCriteri2.Value, "|")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文