[Solved] Extract Last Bracketed Characters from String in Excel VBA


Try this UDF

Function ExtractByRegex(sTxt As String)
With CreateObject("VBScript.RegExp")
    .Pattern = "V\d+(.)?(\d+)?"
    If .Test(sTxt) Then ExtractByRegex = .Execute(sTxt)(0).Value
End With
End Function

Update

Here’s another version in which you can format the output

Sub Test_ExtractByRegex_UDF()
MsgBox ExtractByRegex("A9 V2.3 8.99")
End Sub

Function ExtractByRegex(sTxt As String)
With CreateObject("VBScript.RegExp")
    .Pattern = "V\d+(.)?(\d+)?"
    If .Test(sTxt) Then sTxt = Replace(.Execute(sTxt)(0).Value, "V", "")

    If Not InStr(sTxt, ".") Then sTxt = sTxt & "." & "000"
    ExtractByRegex = Split(sTxt, ".")(0) & IIf(InStr(sTxt, "."), ".", "") & Format(Split(sTxt, ".")(1), "000")
End With
End Function

4

solved Extract Last Bracketed Characters from String in Excel VBA