How do you execute a regular expression in Excel?

后端 未结 2 1624
借酒劲吻你
借酒劲吻你 2020-12-11 12:09

I am trying to use the following expression to locate a pattern of text in my Excel data. The goal is to then remove the text once it is located.

/.([0-9]+[]?x[]

2条回答
  •  执念已碎
    2020-12-11 13:12

    I have made a User Defined Function to run a regex search and display the final match in the cell.

    =udfRegEx([Cell you want to find the expression],[Cell with the regular expression you want to use])
    

    You need to open the Visual Basic editor and put the following code into a Module:

    Function udfRegEx(CellLocation As Range, RegPattern As String)
    
    Dim RegEx As Object, RegMatchCollection As Object, RegMatch As Object
    Dim OutPutStr As String
    
        Set RegEx = CreateObject("vbscript.regexp")
        With RegEx
            .Global = True
            .Pattern = RegPattern
        End With
    
            OutPutStr = ""
            Set RegMatchCollection = RegEx.Execute(CellLocation.Value)
            For Each RegMatch In RegMatchCollection
                OutPutStr = OutPutStr & RegMatch
            Next
            udfRegEx = OutPutStr
    
        Set RegMatchCollection = Nothing
        Set RegEx = Nothing
        Set Myrange = Nothing
    
    End Function
    

    Also don't forget to add the Reference for Microsoft VBScript Regular Expressions 5.5

提交回复
热议问题