0

I found the following StackOverflow question.
How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

I added "Microsoft VBScript Regular Expressions 5.5" to my references in the VBA interface, and typed in 12abc in cell A1 as in the example. I then did Insert -> Module and copied the following code to the code box:

Function simpleCellRegex(Myrange As Range) As String
    Dim regEx As New RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "^[0-9]{1,3}"

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            simpleCellRegex = regEx.Replace(strInput, strReplace)
        Else
            simpleCellRegex = "Not matched"
        End If
    End If
End Function

When I type =simpleCellRegex(A1) in cell B1 I get the error

Compile error: method or data member not found

In the debugger the first line of the code is highlighted.

Someone in a comment to an answer mentioned that an error might occur if the code is in ThisWorkbook, but mine is in Modules.

Laurel
  • 5,965
  • 14
  • 31
  • 57
Antsushi
  • 167
  • 10

2 Answers2

1

Consider using Late Binding by calling the CreateObject Function. It avoids possible bugs associated with mistakenly referencing unneeded libraries or cross referencing them. See below:

Public Function simpleCellRegex(ByRef Myrange As Range) As String

    Dim regEx As Object
    Dim strPattern As String
    Dim strInput As String
    Dim strReplace As String
    Dim strOutput As String

    strPattern = "^[0-9]{1,3}"

    'late binding
    Set regEx = CreateObject("VBScript.RegExp")

    If strPattern <> "" Then
        strInput = Myrange.Value
        strReplace = ""

        With regEx

            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.test(strInput) Then
            simpleCellRegex = regEx.Replace(strInput, strReplace)
        Else
            simpleCellRegex = "Not matched"
        End If

    End If
End Function
rickmanalexander
  • 599
  • 1
  • 6
  • 17
  • Thank you, this seems to be useful for any future endeavors in VBA. Oddly (or maybe not, I don't know), when I turn on Regular Expressions 1.0 after having them turned off (so both 5.5 and 1.0 are working) the code executes just fine. But if I first select 1.0 and then turn on 5.5 I get the error. – Antsushi Oct 10 '19 at 18:23
  • 1
    @Antsushi You can set the priority order of references, the first clicked has a higher priority so any conflicting references now default to 1.0. You can change this with the arrows to the right of the list. – Warcupine Oct 10 '19 at 18:31
  • 1
    @Antsushi ^^^^ what @Warcupine said. `CreateObject` in my opinion is still the better way to go in the long run. – rickmanalexander Oct 10 '19 at 18:34
0

Make sure the reference was added to the module and workbook where you are trying to execute the function

the module must be selected in order to add the reference to that specific module: enter image description here

here it shows that my Module1 has the reference "Microsoft VBScript Regular Expressions 5.5"

and if said reference is unchecked, you'll get this error: Compile error: method or data member not found

this is the result I got:

enter image description here

mdelapena
  • 185
  • 1
  • 14