[COLOR="Blue"]Function[/COLOR] Separate(Str [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
[COLOR="Blue"]With[/COLOR] CreateObject("VBScript.RegExp")
.Pattern = "(\d+)-(\d+)-(\d+)"
Separate = .Execute(Str)(0)
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
Of course, we can do this without resorting to RegEx...Rick Rothstein To avoid "ifs", more safer to use regex:
Code:[COLOR=blue]Function[/COLOR] Separate(Str [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]) [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=blue]With[/COLOR] CreateObject("VBScript.RegExp") .Pattern = "(\d+)-(\d+)-(\d+)" Separate = .Execute(Str)(0) [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR] [COLOR=blue]End[/COLOR] [COLOR=blue]Function[/COLOR]
Function Separate(Strng As String) As String
Dim X As Long, OpenParen As Long, CloseParen As Long, Txt As String
OpenParen = InStr(Strng, "(")
Do While OpenParen
CloseParen = InStr(OpenParen, Strng, ")")
Txt = Mid(Strng, OpenParen + 1, CloseParen - OpenParen - 1)
If Not Txt Like "*[!0-9-]*" Then
If Txt Like "*-*-*" Then
Separate = Txt
Exit Function
End If
End If
OpenParen = InStr(OpenParen + 1, Strng, "(")
Loop
End Function
But then the current RegExp function also fails with the following two examples.I tried on your code with "( 5-40-528 )" and it failed.
[COLOR="Blue"]Function[/COLOR] Separate(Str [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]) [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
[COLOR="Blue"]Dim[/COLOR] mc [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Object[/COLOR]
[COLOR="Blue"]With[/COLOR] CreateObject("VBScript.RegExp")
.Pattern = "\(\s*((\d+)-(\d+)-(\d+))\s*\)"
[COLOR="Blue"]Set[/COLOR] mc = .Execute(Str)
[COLOR="Blue"]If[/COLOR] mc.Count > 0 [COLOR="Blue"]Then[/COLOR]
Separate = mc(0).SubMatches(0)
[COLOR="Blue"]Else[/COLOR]
Separate = ""
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Function[/COLOR]
I presume you meant my code. No problem as the fix is easy (just needed to add a Trim function call)...I tried on your code with "( 5-40-528 )" and it failed.
Function Separate(Strng As String) As String
Dim X As Long, OpenParen As Long, CloseParen As Long, Txt As String
OpenParen = InStr(Strng, "(")
Do While OpenParen
CloseParen = InStr(OpenParen, Strng, ")")
Txt = Trim(Mid(Strng, OpenParen + 1, CloseParen - OpenParen - 1))
If Not Txt Like "*[!0-9-]*" Then
If Txt Like "*-*-*" Then
Separate = Txt
Exit Function
End If
End If
OpenParen = InStr(OpenParen + 1, Strng, "(")
Loop
End Function
I haven't looked at the linked file but it sounds like we are trying to extract from parentheses at the end of a string. If so, then this may be a worthwhile vba approach, rather than attacking it row by row.
<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Extraction()<br>****<SPAN style="color:#00007F">Dim</SPAN> LR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>****<br>****<SPAN style="color:#00007F">Const</SPAN> SourceCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "A" <SPAN style="color:#007F00">'<-- Change to suit</SPAN><br>****<SPAN style="color:#00007F">Const</SPAN> DestCol <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "J"** <SPAN style="color:#007F00">'<-- Change to suit</SPAN><br>****<br>****Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>****LR = Cells(Rows.Count, SourceCol).End(xlUp).Row<br>****<SPAN style="color:#00007F">With</SPAN> Cells(1, DestCol).Resize(LR)<br>********.Value = Cells(1, SourceCol).Resize(LR).Value<br>********.Replace What:="*(", Replacement:="", LookAt:=xlPart, _<br>************SearchFormat:=False, ReplaceFormat:=False<br>********.Replace What:=")", Replacement:="", LookAt:=xlPart, _<br>************SearchFormat:=False, ReplaceFormat:=False<br>****<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>****Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>