Question : Excel VBA: isolate text in parentheses

As part of a VBA macro I'm writing in Excel 2007, I need to split text values that contain parentheses. Starting with the text in the cell, which I'll call CurrentText, I want to generate two more strings: NoParens and InParens. NoParens should be the text minus any substring that was in parentheses (and adjusted for spacing); InParens should be the text that was contained in the parentheses.

Examples might be helpful. Let's say CurrentText = "Some Sample Text (SST)". In this case, NoParens should be "Some Sample Text", and InParens should be "SST".

Or, if CurrentText = "More Sample Text (MST) here", then NoParens should be "More Sample Text here", and InParens should be "MST".

I can't quite figure this one out, and would be very grateful if anyone could lend a hand. Thanks!

Answer : Excel VBA: isolate text in parentheses

try this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
Sub getSubString()

Dim InParens As String, NoParens As String, total As String
Dim leftParens As Integer, rightParens As Integer

total = "Some Sample Text (SST)"

leftParens = InStr(total, "(")
rightParens = InStr(total, ")")
difference = rightParens - leftParens - 1

InParens = Mid$(total, (leftParens + 1), difference)
NoParens = Replace(total, "(" & InParens & ")", "")

MsgBox (InParens & vbCrLf & vbCrLf & NoParens)

End Sub
Random Solutions  
 
programming4us programming4us