关闭那个蝙蝠让我说我是VBA(Excel)的新手:D
无论如何,我有一个输入 Vigil,RR#FM3434,Vigil
我需要输入(来自单元格)分为
RR#FM3434
˚F
中号
3434
并将所有4个部分放入自己的变量中,以便我可以使用它们。 RR#是唯一一个保持相同的F和M以及数字变化很好的数字,数字可以更长,但很好地停留空间或,
所以会找到
RR#LM5464
放入变量然后删除" RR#"然后删除将它们放在变量中的数字,然后将L和M分开并将它们放入自己的变量中
如果它没有告诉硬plz留下关于编码的注释,提供有关正在发生的事情的一些信息:D总是为任何帮助你可以给我任何帮助
答案 0 :(得分:1)
我认为你使用正则表达式是正确的,但我建议使用单个正则表达式,然后使用Submatches拉出这些部分。例如:
Option Explicit
Function SDI(S As String)
Dim RE As Object, allMatches As Object, SM As Object
Const sPat As String = "\b(RR#)\s+([A-Z])([A-Z])(\d+)"
Dim vResult(0 To 3) As Variant
Dim I As Long
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.IgnoreCase = True 'or false, depending on your requirements
.Pattern = sPat
If .test(S) = True Then
Set allMatches = RE.Execute(S)
Set SM = allMatches(0).submatches
For I = 0 To SM.Count - 1
vResult(I) = SM(I)
Next I
End If
End With
MsgBox Join(vResult, ".....")
End Function
答案 1 :(得分:0)
这是我能想到的最好的lol
Sub ExtractSDI()
Dim text As String
Dim result As String
Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
text = "RR# FM454"
RE.Pattern = "(RR# [a-z]{2}\d+)"
RE.Global = True
RE.IgnoreCase = True
Set allMatches = RE.Execute(text)
If allMatches.Count <> 0 Then
'full name
result = allMatches.Item(0).SubMatches.Item(0)
'remove the RR#
text = result
RE.Pattern = "([a-z]{2}\d+)"
Set allMatches = RE.Execute(text)
result2 = allMatches.Item(0).SubMatches.Item(0)
' Keep only numbers
text = result2
RE.Pattern = "(\d+)"
Set allMatches = RE.Execute(text)
result3 = allMatches.Item(0).SubMatches.Item(0)
' Gives me the FM
text = result2
RE.Pattern = "([a-z]{2})"
Set allMatches = RE.Execute(text)
result4 = allMatches.Item(0).SubMatches.Item(0)
' Gives me the F
text = result4
RE.Pattern = "([a-z]{1})"
Set allMatches = RE.Execute(text)
result5 = allMatches.Item(0).SubMatches.Item(0)
' Gives me M
result6 = Replace(result4, result5, "")
End If
MsgBox (result & "....." & result2 & ".........." & result3 & "....." & result4 & "..." & result5 & "....." & result6)
End Sub