您不需要正则表达式来删除第一个逗号后的所有内容。下面的函数做同样的事情:
MID(A1,1,SEARCH(",",A1)-1)
也就是说,以下内容至少适用于 Office 365(未在早期版本上测试):
Public Function RegExpGroup(R As String, S As String, IMatch As Integer, IGroup As Integer) As Variant
Dim RegExp As Object, Matches As Object, SubMatches As Object
Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = R
End With
Set Matches = RegExp.Execute(S)
If Matches.Count >= IMatch Then
Set SubMatches = Matches.Item(IMatch - 1).SubMatches
If SubMatches.Count >= IGroup Then
RegExpGroup = SubMatches.Item(IGroup - 1)
Else
RegExpGroup = CVErr(xlErrValue)
End If
Else
RegExpGroup = CVErr(xlErrValue)
End If
End Function
现在,值为:
以及 A4、A5 中的公式:
=RegExpGroup(A2,A1,1,1),C1:D2,2,FALSE)
=IFERROR(VLOOKUP(RegExpGroup(A2,A1,1,1),C1:D2,2,FALSE),"Not found")
你得到了预期的结果。