让我们说
在一个范围内A1
8厘米书+ 13厘米尺+ 0.34厘米纸,然后我如何把8+13+0.34加在细胞内,然后= 21.34cm
然后将总结放在A2中
-更新问题-更新问题
对不起,我必须把问题弄清楚!8厘米的书+13厘米的尺子+尺子version#2 +0.34厘米的纸
我在想,它是否仍然适用于所有主人的代码?
这个问题困扰我一个星期。希望能得到报应!谢谢!!
发布于 2018-10-17 09:58:44
试试这个简单的基于regex的UDF。
Option Explicit
Function sumNums(str As String) As Double
Dim n As Long
Static rgx As Object, cmat As Object
If rgx Is Nothing Then
Set rgx = CreateObject("VBScript.RegExp")
End If
With rgx
.Global = True
.MultiLine = True
.Pattern = "(\-?\d*\.?\d+)"
If .test(str) Then
Set cmat = .Execute(str)
For n = 0 To cmat.Count - 1
sumNums = sumNums + CDbl(cmat.Item(n))
Next n
End If
End With
End Function
发布于 2018-10-17 10:19:13
下面是一个用户定义的函数,只使用字符串索引:
Function SumNumbers(s As String) As Currency
Dim i As Long
Dim j As Long
For i = 1 To Len(s)
If InStr("-0123456789.", Mid(s, i, 1)) Then
j = i
Do
i = i + 1
Loop While InStr("0123456789.", Mid(s, i, 1))
SumNumbers = SumNumbers + CCur(Mid(s, j, i - j))
End If
Next
End Function
将其放入新模块中的Visual编辑器(Shift+F11)中(右键单击项目,选择“插入.”)和“模块”)。
在工作表中使用公式,例如在单元格B1中:
=SumNumbers(A1)
注:我更喜欢使用Currency
和CCur
来避免浮点错误。它预计数字最多有4个小数点,并且支持十进制分隔符之前的14位数字。
发布于 2018-10-17 10:19:27
另一个解决办法是:
Function cal_cm(cl As Range) As Double
Dim i%, testVal$, result$
result = "="
For i = 1 To Len(cl)
testVal = cl.Characters(i, 1).Text
If "0123456789+." Like "*" & testVal & "*" Then
result = result & testVal
End If
Next i
cal_cm = Evaluate(result)
End Function
测试
https://stackoverflow.com/questions/52851767
复制相似问题