2013-01-11 8 views
0

안녕하세요! 문제가 있습니다 : 선택한 텍스트 부분. 할당은 다음과 같습니다 : ,. 예 : 서적 100, 연필 20, ... 행에있는 숫자의 합계를 찾고 끝에 쓰고 싶습니다. 책 100, 핸들 20 $ 120 이 문제를 해결하려고하지만, 가지 않습니다. 예를 들어, 문자열의 문자에 액세스하는 방법이 명확하지 않습니다. 는 그렇게 :연속 번호의 합계 (Word의 VBA)

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

그러나 문자열 (I)와 같은 기능이 있어야합니다 ... 아주 좋은하지 않습니다.

답변

0

정규 표현식 사용은 어떻습니까? Microsoft VBScript Regular Expressions 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 
+0

친애하는 sigil! 당신은 그렇게 빨리 해냈습니다! 나는 그것을 이해하고 싶다. 그래서 당신에게 코드에 대한 몇 가지 코멘트를 추가 할 수있다. 또는 book \ site에 대한 링크를 어디에서 읽을 수 있는가? 이것은 훌륭합니다! – tbutton

+0

다른 사람들의 경우 : Microsoft VBScript 정규식 5.5에 대한 참조 (도구 - 참고 자료) – tbutton

+0

방금 ​​내 코드에 몇 가지 설명을 추가했습니다. 일반적으로 정규 표현식에 대한 참조로 [this link] (http://www.regular-expressions.info/reference.html)를 사용하고 VBA에서 regexp를 사용하는 것에 대한 특별한 제한 사항은 [here] (http : // www.regular-expressions.info/vbscript.html). 튜토리얼 [here] (http://www.regular-expressions.info/tutorial.html). – sigil

관련 문제