My VBA function should take a string referring to a range of units (i.e. "WWW1-5"), and then return another string.
I want to take an argument and put it in a string separated by a comma, Therefore, it "WWW1-5"should become"WWW1, WWW2, WWW3, WWW4, WWW5".
It will not always be a single digit. For example, I may need to separate "XXX11-18"or something like that.
I have never used regular expressions, but I keep trying different things to make this work, and it seems that it finds only 1 match instead of 3.
Any ideas? Here is my code:
Private Function split_group(ByVal group As String) As String
Dim re As Object
Dim matches As Object
Dim result As String
Dim prefix As String
Dim startVar As Integer
Dim endVar As Integer
Dim i As Integer
Set re = CreateObject("vbscript.regexp")
re.Pattern = "([A-Z]+)(\d+)[-](\d+)"
re.IgnoreCase = False
Set matches = re.Execute(group)
Debug.Print matches.Count
If matches.Count <> 0 Then
prefix = matches.Item(0)
startVar = CInt(matches.Item(1)) 'error occurs here
endVar = CInt(matches.Item(2))
result = ""
For i = startVar To endVar - 1
result = result & prefix & i & ","
Next i
split_group = result & prefix & endVar
Else
MsgBox "There is an error with splitting a group."
split_group = "ERROR"
End If
End Function
I tried setting global = true, but realized that this was not the problem. The error actually occurs in the comment line, but I assume that it was only 1 match.
Google, , , , , RE, , , , .
!