2

Hello I'm having some issue tring to get the regular expression code below to work. I'm getting a application-undefine or Object-Undefine error

the text box is on user form.

the error is occurring on the line "Set allMatches = regEx.Execute(TextBox1.Text)" not sure what I missed.

Dim regEx As Object

Dim allMatches As Object

Set regEx = CreateObject("VBScript.RegExp")
With regEx
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2)[AM|PM]"
        .Global = True
End With

Set allMatches = regEx.Execute(TextBox1.Text)


If allMatches.Count <> 0 Then
    result = allMatches.Item(0).submatches.Item(0)
End If
3
  • Just a crazy thought. Is TextBox1.Text valid? Can you successfully do a MsgBox on it? Commented Aug 20, 2013 at 22:02
  • What version of Excel and what textbox control are you using? IE ActiveX Commented Aug 20, 2013 at 22:05
  • the textbox is just on a form and yes I can access with a MsgBox, and I'm using excel 2010 with XP Commented Aug 20, 2013 at 23:39

3 Answers 3

2

Ok after some googling and looking, I found the problem: its with the pattern:

 .pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2**)** [AM|PM]"

it turns out you will get the 5017 error if the pattern is not valid.

by changing the ")" to the proper closing "}" the error was solved.

 .pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2**}** [AM|PM]"

I would thought that if the pattern did not match then you would get a false return, not so..

Sign up to request clarification or add additional context in comments.

1 Comment

There's probably a difference between a non-matching pattern, and a syntactically invalid pattern. It sounds like you had the latter, but good job for solving your own question!
0

So it is possible that you are accessing the Textbox incorrectly. Without knowing how you setup the textbox I am going to guess that its held in the shapes collection. You could look for the textbox and then set the allMatches such as below

Dim shp As Shape
'loop through the shapes on the sheet - assuming you are working with sheet 1
For Each shp In ThisWorkbook.Sheets(1).Shapes
    If shp.Name = "TextBox1" Then
         Set allMatches = regEx.Execute(shp.TextFrame2.TextRange.Text)
    End If
Next

1 Comment

Hi Sorceni, the text box is place right on the form it does not nest inside any frame or or shape..
0

Assuming you are accessing the textbox correctly, the [AM|PM] poses a problem in my mind.
It seems to me this would match one character: A, M or P, or M.

If it were me I would use [A|P]M- then the first letter could be A or P and the second letter must be M.
That would also assume it is only looking for capital letters.
To include lowercase, ([Aa]|[Pp])[Mm] OR maybe better [AaPp][Mm].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.