Excel函数查找stringexpression式的“深度”?

我有一个Python中的algorithm,基本上可以帮助find一个函数的“深度”:

f(a) has a depth of 1 f(g(h(a,b,c),d)),e) has a depth of 3 

伪algorithm是这样的:

 Run through all the characters from left to right create a variable "depth" for each open parenthesis increment depth for each closing parenthesis decrement depth At the end, the depth of the expression was the max value taken by the variable depth. 

我想知道如果这样的逻辑是可以实现在Excel中不使用VBA。

尝试下面的UDF()

 Public Function Depth(r As Range) As Long Dim v As String, CH As String Depth = 0 v = r.Text kount = 0 For i = 1 To Len(v) CH = Mid(v, i, 1) If CH = "(" Then kount = kount + 1 If CH = ")" Then kount = kount - 1 Depth = Application.WorksheetFunction.Max(kount, Depth) Next i End Function 

用户定义的函数(UDF)非常易于安装和使用:

  1. ALT-F11调出VBE窗口
  2. ALT-I ALT-M打开一个新的模块
  3. 粘贴东西,closuresVBE窗口

如果保存工作簿,则UDF将随之保存。 如果您在2003年以后使用的是Excel版本,则必须将该文件另存为.xlsm而不是.xlsx

要删除UDF:

  1. 如上所示调出VBE窗口
  2. 清除代码
  3. closuresVBE窗口

从Excel中使用UDF:

=深度(A1)

要了解有关macros的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

有关UDF的具体信息,请参阅:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

macros必须启用这个工作!

在这里输入图像说明