Link to home
Start Free TrialLog in
Avatar of Gabriel_Brienza
Gabriel_Brienza

asked on

Another string problem .Extracting numbers from Brackets


strings formatting drives me crazy.


I always have strings
like

mystring="Deposit Account (22.50)
or
mystring="Mycredit Account(250.50)

Basically the amaount within the brackets can be anything.

How can i get the amount within the brackets?

do I have to use a split or instr can somebody give me a solution?

thanks
vbnetuk
Avatar of Steiner
Steiner

Try:

msgbox val(mid(mystring,instr(mystring,"(")+1))

This will look for the opening bracket and then extract any value it can find there.
Try this

Dim Arr
dim Value

Arr = split(mystring, "(")
value = cdbl(Arr(1)
erase Arr

msgbox value
will the numbers that you want to 'extract' ALWAYS be enclosed in (....)s.   Do you care if the text says "Deposit" or "MyCredit"?

Why is the text formatted in that way?  Do you have any control over that?

AW
Hi Gabriel_Brienza,

Use Instr and MID

value = Mid(MyString,Instr(MyString,"(") + 1,Instr(MyString,")") - Instr(MyString,")") + 1)

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
Dear Gabriel,

You will have to use Instr and the either split, left or right or mid.

You would get the position of ( and ) respectively using Instr and then use Mid to extract the value between the (). If you plan to do arithematic calculatins then you should convert using CInt, or CDbl


Regards,

G. Nagraj
Avatar of Gabriel_Brienza

ASKER

Thanks for all your answers

Arthur
I dont have control the way it is formatted
I just need the numbers in the brackets and convert to currency.


Steiner
it works but i will need to convert to currency

TimCotte
it extract only a 2

EDDYKT
Tried but didnt work



Try this

Dim Arr
Dim Value

Arr = Split(mystring, "(")
Value = Val(Arr(1))
Erase Arr
Another try with the brackets in the right places:

Value = Mid(MyString, InStr(MyString, "(") + 1, InStr(MyString, ")") - InStr(MyString, "(") - 1)
ASKER CERTIFIED SOLUTION
Avatar of Steiner
Steiner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everybody  for your help
and suggestions.
In the end more than one answer was good but Steiner was the first correct one

Thanks again