怎样用公式从字符串中提取数字并相加?
=sumspecial(A1:A5)
以下是该自定义函数的代码——
Option Explicit
Function sumspecial(rng As Range) As Double
If rng Is Nothing Then Exit Function
Dim objRegExp As Object, k As Range
Set objRegExp = CreateObject("VBScript。 RegExp")
With objRegExp
。Pattern = "[\u4e00-\u9fa5]|[A-Za-z]"
。Ignorecase = True
。Global =...全部
=sumspecial(A1:A5)
以下是该自定义函数的代码——
Option Explicit
Function sumspecial(rng As Range) As Double
If rng Is Nothing Then Exit Function
Dim objRegExp As Object, k As Range
Set objRegExp = CreateObject("VBScript。
RegExp")
With objRegExp
。Pattern = "[\u4e00-\u9fa5]|[A-Za-z]"
。Ignorecase = True
。Global = True
For Each k In rng
sumspecial = sumspecial + Val(。
Replace(k。Value, ""))
Next
End With
Set objRegExp = Nothing
End Function
要适用不同的提取需求,只需要改变
。
Pattern = "[\u4e00-\u9fa5]|[A-Za-z]"
中的正则表达式就可以了。收起