0

再会!有一个问题:选定的一段文字。在配置中如下: , 。示例:book 100,pencil 20,...我想求行中数字的总和写在最后,即book 100,手柄20 $ 120 我正在尝试解决这个问题,但不去......不清楚,例如,如何访问字符串中的字符。这样做了:

While i <> EndOfText
Char = Mid (AllSelectionText, i, 1)
If Char> = "0" And Char <= "9" Then
...
End If
i = i 1
Wend

但它不是很好......必须有一个像字符串(i)这样的函数。

4

1 回答 1

0

使用正则表达式怎么样?您需要添加对 Microsoft VBScript 正则表达式 5.5 的引用,然后您可以使用以下内容(未经测试):

Function sumOfNumbersInText(text As String) As Long

Dim rgx As New RegExp  'creates a new instance of the Regular Expression object
Dim matches As MatchCollection
Dim mtch As Match
Dim sum As Long

sum = 0
rgx.Pattern = "\d+"  'pattern for a sequence of digits
rgx.Global = True    'find all matches within text
Set matches = rgx.Execute(text) 'get collection of all substrings matching the pattern
For Each mtch In matches
    sum = sum + CLng(mtch.Value) 'change each substring into a number and add to sum
Next

sumOfNumbersInText = sum

End Function
于 2013-01-11T19:11:04.200 回答