在数字中放置括号

我需要在Excel工作表的单元格中的string中加上括号。

例如,说我给:

913/(300+525)

我需要得到这个回报:

[913]/([300]+[525])

方程非常简单,只需要处理+ - * / ( )字符。

我试图通过使用MID函数逐string来循环,但是我无法使循环工作正常,并最终得到一个混乱的随机括号和数字。 我也考虑过使用正则expression式,但是我从来没有使用它们,也不知道这是否是一个好的应用。

请让我知道,如果你需要别的东西。 感谢您的时间!

他们可以体面长。 这是另一个例子:

我有:

(544+(1667+1668+1669+1670+1671+1672+1673)-1674)

但是我需要:

([544]+([1667]+[1668]+[1669]+[1670]+[1671]+[1672]+[1673])-[1674])

我只是扔在一起,但它应该工作

 Function generateBrackets(Equation As String) As String Dim temp As String Dim brackets As Boolean Dim x 'If we're using Option Explicit, or just to be safe For x = 1 To Len(Equation) If Not IsNumeric(Mid(Equation, x, 1)) And brackets = False Then temp = temp & Mid(Equation, x, 1) ElseIf Not IsNumeric(Mid(Equation, x, 1)) And brackets = True Then temp = temp & "]" & Mid(Equation, x, 1) brackets = False ElseIf IsNumeric(Mid(Equation, x, 1)) And brackets = False Then temp = temp & "[" & Mid(Equation, x, 1) brackets = True ElseIf IsNumeric(Mid(Equation, x, 1)) And brackets = True Then temp = temp & Mid(Equation, x, 1) End If Next x generateBrackets = temp End Function 

在这里输入图像说明

这是一个迎合十进制数字的方法。

 '~~> Add here whatever operators your equation '~~> is likely to have Const delim As String = "+()-/" Sub Sample() Dim MyAr Dim sSamp As String sSamp = "(5.44+(16.67+1668+1669+1670+1671+1672+1673)-1674)" MyAr = Split(GetNewString(sSamp)) For i = 0 To UBound(MyAr) sSamp = Replace(sSamp, MyAr(i), "[" & MyAr(i) & "]") Next i Debug.Print sSamp End Sub Function GetNewString(s As String) As String Dim sTemp As String sTemp = s For i = 1 To Len(delim) sTemp = Replace(sTemp, Mid(delim, i, 1), " ") Next i Do While InStr(1, sTemp, " ") sTemp = Replace(sTemp, " ", " ") Loop GetNewString = Trim(sTemp) End Function 

input

"(5.44+(16.67+1668+1669+1670+1671+1672+1673)-1674)"

产量

([5.44]+([16.67]+[1668]+[1669]+[1670]+[1671]+[1672]+[1673])-[1674])