I'm trying to use RegEx in an Excel 2015 macro. I don't know if I'm doing something wrong, but every time I run it, my Excel crashes. Here's the macro:
Sub MakeExplicit()
Dim whitespace As RegExp
Set whitespace = New RegExp
whitespace.Pattern = "\s+"
whitespace.MultiLine = True
whitespace.Global = True
Dim implicit As RegExp
Set implicit = New RegExp
implicit.Pattern = "^\d+-\d+$"
Dim row As range
For Each row In ActiveSheet.UsedRange.Rows
Dim first As range
Set first = row.Cells(1, 1)
Dim str As String
str = first.Text
str = whitespace.Replace(str, Nothing)
If implicit.Test(str) Then 'FIXME here it crashes
Dim FromTo As Variant
FromTo = Split(str, "-")
Dim sFrom, sTo As Integer
sFrom = FromTo(1)
sTo = FromTo(2)
' doplň chybějící číslice
' např [2345, 78] doplní
' na [2345, 2378]
sTo = Left( _
sFrom, _
Len(sFrom) - Len(sTo) _
) + sTo
Dim iFrom, iTo As Integer
iFrom = CInt(sFrom)
iTo = CInt(sTo)
If iFrom > iTo Then _
Err.Raise 42, first.Address, _
"Wrong order of numbers!"
Dim i As Integer
For i = iFrom To iTo
' some more code
Next i
End If
Next row
End Sub
By using the Debugger I found out it crashes when the code reaches "If implicit.Test(str) Then
" implying there's something wrong with RegEx. These are the project's references:
The obvious question is how do I get it to work? VBA is a very ugly language by itself, so I have no preference about how, just making it work is enough.