1

When using this line in Excel VBA:

Cells(a, 20).Value = regexProjet.Execute(Cells(a, 1).Value)(0)

I get a warning saying that either the parameter or the command is invalid.

I have use this line in many places in my code and it worked fine, the format of the cells are all standard (they're strings...).

Anyone could give me some hints of what to look for?

FYI this is how I declared the regex:

Dim regexProjet As Object
Set regexProjet = CreateObject("VBScript.RegExp")
regexProjet.IgnoreCase = True
regexProjet.Pattern = "^   ([a-z]+)(-)([0-9]+)" 'conserve seulement la clé du projet
8
  • If it worked before and you copied the line, then have you set the reference to the RegEx in the application - Tools >> References? Commented Aug 15, 2014 at 13:40
  • what references do I chose if I want to try this out? Commented Aug 15, 2014 at 13:57
  • Refer to How to use regular expression. The link will show answer with steps Commented Aug 15, 2014 at 14:01
  • Thanks, I added the reference but it didn't change anything Commented Aug 15, 2014 at 14:09
  • 1
    You should not need to enable the reference if you're using late binding (you are using late binding). Commented Aug 15, 2014 at 15:09

2 Answers 2

2

You will get that response if your regex does not match your data. To avoid it, using the technique you are using, first do a test to see if your regex matches your string.

e.g:

If regexProjet.test(Cells(a,1).value) then
    Cells(a, 20).Value = regexProjet.Execute(Cells(a, 1).Value)(0)
Else
   ... your error routine
End If

Also, you should note that if you are just trying to match the overall pattern, there is no need for the capturing groups (and they will add execution time, making the regex less efficient).

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

Comments

0

Here is sample code I got to run in Excel 2013 but it's slightly different than what you have. I got the code from [How to regular expressions]: http://support.microsoft.com/kb/818802

Sub testreg()

   Dim regexProjet As New RegExp
Dim objMatch As Match
Dim colMatches   As MatchCollection
Dim RetStr As String

RetStr = ""

regexProjet.IgnoreCase = True
regexProjet.Pattern = "^   ([a-z]+)(-)([0-9]+)"


Set colMatches = regexProjet.Execute(Cells(1, 1).Value)

For Each objMatch In colMatches   ' Iterate Matches collection.
  RetStr = RetStr & " " & objMatch.Value
Next

Cells(1, 20).Value = RetStr

End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.