如何在vlookup中组合正则表达式函数?

时间:2013-10-09 19:51:49

标签: regex excel excel-2007

我有一个VBA正则表达式,我想与VLOOKUP结合使用,但如果与VLOOKUP一起使用,它不会返回基于正则表达式的值。

这是我执行函数时返回的内容 =udfRegEx(A2,B2)

字符串

Microsoft Windows Server 2003,标准版(64位)

正则表达式

^([^,] *)

结果

Microsoft Windows Server 2003

但是,当我执行=IFERROR(VLOOKUP(udfRegEx(A2,RegularExpression!B2),[Sample.xls]Sheet1!$B$2:$E$4177,4,FALSE),0)时,它仍会返回Microsoft Windows Server 2003,标准版(64位)

B2列是正则表达式^([^,]*)

4 个答案:

答案 0 :(得分:0)

尝试使用:

=IFERROR(udfRegEx(VLOOKUP(udfRegEx(A2,RegularExpression!B2),[Sample.xls]Sheet1!$B$2:$E$4177,4,FALSE),RegularExpression!B2),0)

在黑暗中拍摄。

答案 1 :(得分:0)

从Office 365开始,有一个新功能XLookUp,该功能(最终)完成了您要查找的滚刀。此处说明:https://www.excelcampus.com/functions/xlookup-explained/

答案 2 :(得分:0)

您不需要正则表达式来删除第一个逗号之后的所有内容。以下函数执行相同的操作:

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

现在,值如下:

enter image description here

以及A4、A5中的公式:

=RegExpGroup(A2,A1,1,1),C1:D2,2,FALSE)
=IFERROR(VLOOKUP(RegExpGroup(A2,A1,1,1),C1:D2,2,FALSE),"Not found")

你得到了预期的结果。

答案 3 :(得分:-1)

我必须这样做才供个人使用,所以我制作了一个Excel Addin,这是GitHub地址。

https://github.com/BlueTrin/BlueXL

如果您愿意,我可以在需要时托管编译版本。它添加了一个名为BXLookup的函数,该函数支持Regex,您还可以选择执行查找的列,并选择要打印的列。

我为你做了一个二进制文件:

https://bintray.com/bluetrin/BlueXL/BlueXL/0.1.0/view?sort=&order=#

当然,如果您只想使用VBA,这不起作用,但如果您不介意使用插件,GitHub上的电子表格中有一个示例。

请你澄清一下你所拥有的内容:[Sample.xls]Sheet1!$B$2:$E$4177