ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何依计算流程拆分算术表达式?

如何依计算流程拆分算术表达式?

作者:绿色风 分类: 时间:2022-08-17 浏览:85
楼主
herelazy
Q:如何用VBA按计算流程拆分算术表达式:(((1+2)/0.1+((-3)+5)/(-0.1))/-0.1+(4+5)*3)/0.2?

假设A0=1+2;则B0=A0/0.1或者B0=(1+2)/0.1
按照括号,乘除,加的顺序(无减法);
最后希望能得到Y=(A0,A1,A2……)。

A:
  1. Sub test()
  2. Dim Str$, mMatch, Matches, Arr, N&, I&, T&
  3. Str = "(((1+2)/0.1+((-3)+5)/(-0.1))/-0.1+(4+5)*3)/0.2"
  4. Arr = Array("\-*\d*\.*\d+[\*\/]\-*\d*\.*\d+", "(?![\*\/])\-*\d*\.*\d+[\+\-]\-*\d*\.*\d+(?=[^\*\/])", "\([^\(\)]+?\)")
  5. Columns(1).Clear
  6. Cells(1, 1) = Str
  7. N = 2
  8. With CreateObject("vbscript.regexp")
  9.     .Global = True
  10.     Do
  11. Again:
  12.         For I = LBound(Arr) To UBound(Arr)
  13.             .Pattern = Arr(I)
  14.             If .test(Str) Then
  15.                 Do
  16.                     .Pattern = Arr(I)
  17.                     Set Matches = .Execute(Str)
  18.                     For Each mMatch In Matches
  19.                         If I = UBound(Arr) Then
  20.                             For T = LBound(Arr) To UBound(Arr) - 1
  21.                                 .Pattern = Arr(T)
  22.                                 If .test(Str) Then GoTo Again
  23.                             Next T
  24.                         End If
  25.                         Str = Replace(Str, mMatch.Value, Evaluate(mMatch.Value), , 1)
  26.                         Cells(N, 1) = Str
  27.                         If IsNumeric(Str) Then End
  28.                         N = N + 1
  29.                     Next mMatch
  30.                 Loop Until Not .test(Str)
  31.             End If
  32.         Next I
  33.     Loop
  34. End With
  35. End Sub
效果图:

 

如何依计算流程拆分算术表达式?.rar

2楼
xyf2210
学习

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap