【问题标题】:Count number of arguments to Excel formula in VBA在 VBA 中计算 Excel 公式的参数数量
【发布时间】:2010-04-12 02:03:33
【问题描述】:

我需要使用 VBA 来确定传递给 Excel 公式的参数数量。例如,假设一个单元格包含公式 =MyFunc($A$1, "xyz", SUM(1,2,COUNT(C1:C12)), IF(B1>2,1,0))。那么计数器函数应该返回 4。VBA 是否包含任何内置函数,或者有人有一个可以计算这个的正则表达式示例?

更新:

感谢 user225626 和 Charles。我发现的一个问题是当引用的字符串参数包含逗号时;这些逗号导致参数计数增加。我已经修改了 Charles 的代码来解决这个问题。

Public Function CountFormulaArguments(sStr As String) As Integer
    Dim strChar As String
    Dim nArgs As Integer
    Dim n, nLParen, nCommas As Integer
    Dim blArray, bQuote As Boolean
    
    nLParen = 0
    nArgs = 0
    For n = 1 To Len(sStr)
        strChar = Mid(sStr, n, 1)
        If strChar = "(" Then
            nLParen = nLParen + 1
            If nLParen = 1 Then nArgs = nArgs + 1
        ElseIf strChar = ")" Then nLParen = nLParen - 1
        ElseIf nLParen = 1 And strChar = "{" Then blArray = True
        ElseIf blArray And strChar = "}" Then blArray = False
        ElseIf Not bQuote And strChar = """" Then bQuote = True
        ElseIf bQuote And strChar = """" Then bQuote = False
        ElseIf nLParen = 1 And Mid(sStr, n, 1) = "," And Not blArray And Not bQuote Then nCommas = nCommas + 1
        End If
    Next
    nArgs = nArgs + nCommas
    
    CountFormulaArguments = nArgs
End Function

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    扩展Test01 以允许在一个语句中使用数组常量和多个函数调用:

    =SUM({1,2,3,4,5},{1,2})+SUM({1,2,3,4,5})<br/><br/>
    

    代码:

    Sub Test02()
        sStr = Sheets("Sheet1").Range("A2").Formula
    
        For n = 1 To Len(sStr)
            strChar = Mid(sStr, n, 1)
            If strChar = "(" Then
                nLParen = nLParen + 1
                If nLParen = 1 Then nArgs = nArgs + 1
            End If
            If strChar = ")" Then nLParen = nLParen - 1
    
            If nLParen = 1 And strChar = "{" Then blArray = True
            If blArray And strChar = "}" Then blArray = False
            If nLParen = 1 And Mid(sStr, n, 1) = "," And Not blArray Then nCommas = nCommas + 1
        Next
        nArgs = nArgs + nCommas
    
        MsgBox nArgs
    End Sub
    

    【讨论】:

    • Here到达这篇文章:)
    【解决方案2】:
    Sub Test01()
     sStr = Sheets("Sheet1").Range("A1").Formula
    
     For n = 1 To Len(sStr)
      If Mid(sStr, n, 1) = "(" Then nLParen = nLParen + 1
      If Mid(sStr, n, 1) = ")" Then nLParen = nLParen - 1
    
      If nLParen = 1 And Mid(sStr, n, 1) = "," Then nCommas = nCommas + 1
     Next
     nArgs = nCommas + 1
    
     MsgBox nArgs
    End Sub
    

    【讨论】:

      【解决方案3】:

      UBOUND(数组)

      为什么不直接在参数上使用内置的 UBOUND() 函数来确定有多少已馈送到用户定义的函数?

      这很简单,但需要您至少设置一个变量才能获得无限计数。以下是我为我的目的而写的:

          Function My_Func(ParamArray others() as variable)
      
          n = UBound(others) + 1
      
          'insert code here
      
          howmanyargumentsinmyfunc = n
      
          End Function
      

      注意:

      • 'others' 表示输入我的函数的参数数量不限
      • 'n' 表示参数的总数(由于 UBound 处理数组的方式,您必须添加一个)

      UBound 和 ParamArray 是关键的内置命令,用于确定有多少变量被传递到函数中。我想,如果您需要从计数中排除某些值,您可以使用某种 countif 轻松地做到这一点。

      我希望这对某人有帮助!

      【讨论】:

        猜你喜欢
        • 2017-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-24
        相关资源
        最近更新 更多