I could not figure out how to find whether a numeral is present in a string and its position and value in EXCEL- VB
Can someone help out?
I could not figure out how to find whether a numeral is present in a string and its position and value in EXCEL- VB
Can someone help out?
This will return you position of first number in a string:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9}, A1&"0123456789"))
If there is no number it will return you value of LEN(A1)+1
Never use Merged Cells in Excel
SEARCH look for defined word in another word. for example:
=SEARCH("p", "example") will return 5 (postition of p)
=SEARCH("ple", "example") will return 5 (position of ple)
=SEARCH("plex", "example") will return #VALUE! (there is no plex word in example)
Now.. Since you looking for ANY number then your word must contain ALL number or you could get #VALUE! error.
Now,
SEARCH({0,1,2,3,4,5,6,7,8,9} will create an array
Look at this:
=SEARCH({"p","z"}, "example") will return 5 (a position of p).
and
=SEARCH({"z","p"}, "example") will return #VALUE! (there is no z in example).
But MIN formula change a result a lot.
MIN formula look for all defined characters and return MIN of positions (that means FIRST position).
So
=MIN(SEARCH({"p","z"}, "example")) will return MIN(5, #VALUE!) and that is #VALUE!
because there is no z in example
Also, =MIN(SEARCH({"p","exam"}, "example")) will retun 1
To avoid errors you can add string "0123456789") after your word to ensure that ALL numbers will apear and you won't get error but numbers that are not included in string will appear at the END of the string.
Let's look for example:
q4354r439f
and formula =MIN(SEARCH({0,1,2,3,4,5,6,7,8,9}, A1&"0123456789"))
will result with =MIN(SEARCH({0,1,2,3,4,5,6,7,8,9}, q4354r439f0123456789))
so result with searching (search where 0,1,2,... appear will be)
=MIN({11, 12, 13, 3,2,4,17,18,19,9})
MIN now return a first position of number, that is 4 at second position.
If there is no number in desired word:
example
it will look at:
=MIN(SEARCH({0,1,2,3,4,5,6,7,8,9}, example0123456789))
and obviously it will return number after lenght after example (7+1=8) because of putting it at the end of example.
=MIN({8,9,10,11,12,13,14,15,16,17}) -> 8 while lenght of example is 7
Last edited by zbor; 11-14-2010 at 10:16 AM.
Thanks once again. Beautiful explanation!
Can we use an expression inside the flower bracket?
Last edited by DonkeyOte; 11-16-2010 at 07:48 AM. Reason: removed unnecessary quote
Only constants are permitted within { } , eg: {A1,B1} is not a valid construct
If by "expression" you mean "regular expression" you might want to elaborate with examples.
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks