您的位置首页百科知识

Excel VBA实现取字符串中数字并求和

Excel VBA实现取字符串中数字并求和

的有关信息介绍如下:

Excel VBA实现取字符串中数字并求和

Excel VBA 利用正则表达式实现取字符串中数字并求和的方法。

ALT+F11打开VBE编辑器,单击菜单:工具-引用。

弹出引用-VBAProject对话框,勾选Microsoft VBScript Regular EXpressions 1.0,点确定。

新建一个模块,输入如下代码:

Sub tsszstrqh()

Dim reg As New RegExp

Dim str As String, t, s, arr

str = Range("a1").Value

With reg

.Global = True

.Pattern = "[^0-9.]"

arr = "=" & Right(.Replace(str, "+"), Len(.Replace(str, "+")) - 1)

.Pattern = "\++"

Range("b1") = .Replace(arr, "+")

End With

End Sub

按ALT+F8,运行tsszstrqh宏,演示结果如下图: