How do you execute a regular expression in Excel?

后端 未结 2 1623
借酒劲吻你
借酒劲吻你 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

    0 讨论(0)
  • 2020-12-11 13:16

    You didn't specify it but I assumed it was using a VBA macro. i don't think you can do regular expression directly in the sheet using formula.

    The following link should help you with regular expression and VBA:

    http://www.regular-expressions.info/vb.html

    Just be sure to add the correct reference "Microsoft VBScript Regular Expressions 5.5"

    Hope this help

    0 讨论(0)
提交回复
热议问题