extract characters from inside brackets in string

3

3lliot

I've got a combo box containing multiple values, ending in (xxx) where xxx is
any number of characters. I want to write the combo box value to a string but
strip out everything outside the brackets (so I just get the xxx characters).

I looked at using a regex, but it seems a little complicated to get them
working in VBA.

Would anyone be able to rattle off a function that will do that for me? I'd
spend ages working it out but I'll bet someone out there can write that stuff
with their eyes shut :)

cheers
-E
 
D

Doug Robbins - Word MVP

Dim str As String
str = Selection.Text
str = Mid(str, InStr(str, "(") + 1)
str = Left(str, Len(str) - 1)


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
3

3lliot

Thanks Doug - this doesn't seem to work though, the last line is throwing an
error.

If my original variable is strType, and the variable I want to write the
cut-down version to is strTypeParsed, would this be correct?

strTypeParsed = strType
strTypeParsed = Mid(strTypeParsed, InStr(strTypeParsed, "(") + 1)
strTypeParsed = Left(strTypeParsed, Len(strTypeParsed) - 1)
 
3

3lliot

Ah I fixed the error, but it's still not doing what I need.

For instance, for an original string of:

Memo (MEM)

I'm getting:

MEM)


For Minutes (MIN)

I'm getting:

s (MIN)


For Configuration Management Plan (CMP)

I'm getting:

nt Plan (CMP)


But for all of these, I just want

MEM
MIN
CMP

respectively.

Is it because there are too many words in the original strings for Mid to
work properly?

cheers,
Elliot
 
D

Doug Robbins - Word MVP

I do not know what you are doing, but if I run the following code

Dim strType As String, strTypeParsed As String
strType = "For Configuration Management Plan (CMP)"
strTypeParsed = strType
strTypeParsed = Mid(strTypeParsed, InStr(strTypeParsed, "(") + 1)
strTypeParsed = Left(strTypeParsed, Len(strTypeParsed) - 1)
MsgBox strTypeParsed

The message box displays CMP

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
3

3lliot

You're right, my bad - I was adding a hyphen to the end of the string
elsewhere which was messing up the result. All works fine now, thanks again

-E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top