Get the value between the brackets Get the value between the brackets vba vba

Get the value between the brackets


VBA code:

cellValue = "V2397(+60)"openingParen = instr(cellValue, "(")closingParen = instr(cellValue, ")")enclosedValue = mid(cellValue, openingParen+1, closingParen-openingParen-1)

Obviously cellValue should be read from the cell.

Alternatively, if cell A1 has one of these values, then the following formula can be used to extrcat the enclosed value to a different cell:

=Mid(A1, Find("(", A1)+1, Find(")",A1)-Find("(",A1)-1)


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 = NothingEnd FunctionSub Test()MsgBox GetParen("V2397(+60)")MsgBox GetParen("Not me")MsgBox GetParen(ActiveSheet.Range("A1"))End Sub


Use InStr to get the index of the open bracket character and of the close bracket character; then use Mid to retrieve the desired substring.

Using InStr$ and Mid$ will perform better, if the parameters are not variants.