Get the value between the brackets

后端 未结 4 1751
感情败类
感情败类 2021-01-12 05:02

I have a column with some stuff that looks like the following string: V2397(+60)

How do I get the value between the brackets? In this case the +60

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 05:41

    I would use a regular expression for this as it easily handles

    • a no match case
    • multiple matches in one string if required
    • more complex matches if your parsing needs evolve

    The Test sub runs three sample string tests

    The code below uses a UDF which you could call directly in Excel as well, ie = GetParen(A10)

    Function GetParen(strIn As String) As String
        Dim objRegex As Object
        Dim objRegMC As Object
        Set objRegex = CreateObject("vbscript.regexp")
        With objRegex
            .Pattern = "\((.+?)\)"
            If .Test(strIn) Then
                Set objRegMC = .Execute(strIn)
                GetParen = objRegMC(0).submatches(0)
            Else
                GetParen = "No match"
            End If
        End With
        Set objRegex = Nothing
    End Function
    
    Sub Test()
    MsgBox GetParen("V2397(+60)")
    MsgBox GetParen("Not me")
    MsgBox GetParen(ActiveSheet.Range("A1"))
    End Sub
    

提交回复
热议问题