我需要在 Excel 工作表的单元格中找到的字符串中的数字添加括号。
例如,假设我得到:
913/(300+525)
我需要得到这个返回:
[913]/([300]+[525])
方程相当简单,只需要处理 + - */( )
字符。
我尝试使用 MID
函数逐个字符地循环字符串,但我无法使循环正常工作,最终得到一堆乱七八糟的随机括号和数字。我也考虑过使用正则表达式,但我以前从未使用过它们,也不知道这是否是一个好的应用程序。
如果您还有其他需要,请告诉我。感谢您的宝贵时间!
它们可以相当长。这是另一个例子:
我有:
(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
关于excel - 将字符串中的数字放在括号中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26662018/