例如,我有这个字符串,它显示为 "IRS150Sup2500Vup"
。它也可以是 "IRS250Sdown1250Vdown"
。
我想提取两个 S 之间的数字。因此对于第一种情况,它将是 150,第二种情况是 250。数字并不总是 3 位数字。它可能会有所不同。
我尝试过的:
Dim pos As Integer
Dim pos1 As Integer
pos = InStr("IRS150Sup2500Vup", "S")
pos1 = InStrRev("IRS250Sdown1250Vdown","S")
在此之后,我不知道如何取出号码。
需要一些关于如何执行此操作的指导。
最佳答案
正如我建议的那样here ,最简单的方法是使用正则表达式。
Sub Test()
Dim r As VBScript_RegExp_55.RegExp
Dim sPattern As String, myString As String
Dim mc As VBScript_RegExp_55.MatchCollection, m As VBScript_RegExp_55.Match
myString = "IRS150Sup2500Vup"
sPattern = "\d+" 'searches for numbers
Set r = New VBScript_RegExp_55.RegExp
r.Pattern = sPattern
Set mc = r.Execute(myString)
For Each m In mc ' Iterate Matches collection.
MsgBox "number: '" & m.Value & "' founded at: " & m.FirstIndex & " length: " & m.Length
Next
End Sub
关于vba - 如何在VBA中找到两个字符之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27913879/