Extract string starting with ... from a cell

wovawefo

New Member
Joined
Oct 27, 2009
Messages
6
Hi!

I'm trying to write a macro to extract a certain text from a cell. The string in the cell is the following usually: VIFIR - kód :k200010006……. What I need is to extract the 10 digit code from it. but it does not always start at the 14th character and not always with a k. Can start with "k", "e", "f", and "a". Dunno if it is possible to find the second "k" or first "e", "f" or "a" in the cell's text, store the positions of it (13th character). Is it? Cause if I could do that, than i might be able to figure out how to extract 10 characters starting from the stored one.

I searched a lot and havent found an answer.

Thanks for the help.

Peter
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hello and welcome to the board,

Would it always be a alphabet followed by nine numeric?

could you post some more sample data?
 
Upvote 0
it is always one letter, than 9 numbers

k200210010, a200220002, f110210001

it always starts with either a "k", an "f", and "a", or an "e"

it does not always start right after ":"

the problem is sometimes they start right after it, sometimes with one space after, sometimes two. and the other thing, the code is not always the last 10 chars of the string.

sweet huh?

thanks
 
Upvote 0
If the first digit of the 10 charcater code is always the first digit of the string then you could extract with this formula

=MID(A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},A1&1234567890))-1,10)
 
Upvote 0
If the number is the last 9 digit number in the string, the CSE formula
=MID(A1, MAX(IF(ISNUMBER(MID(A1,ROW(INDIRECT("1:100")),9)+0), ROW(INDIRECT("1:100")))) - 1, 10)

will work.
This needs to be confirmed with Ctrl-Shift-Enter (Cmd+Return for Mac)
 
Last edited:
Upvote 0
some examples:

VIFIR - kód :k200010006…….
VIFIR - kód : k200010003..
VIFIR - kód : a100010043
 
Last edited:
Upvote 0
All the posted formula will extract the correct string from those examples.

Here's another CSE formula, this one tests for the leading letter in addition to the other criteria.

=MID(A1,MAX(IF(ISNUMBER(FIND(MID($A$1,ROW(INDIRECT("1:100")),1),"kafe")+MID(A1,ROW(INDIRECT("2:101")),9)),ROW(INDIRECT("1:100")),)),10)

If these doen't work could you post some examples of where they fail.
 
Upvote 0
another

Code:
Function xtract(ByVal ref As String) As String
With CreateObject("VBScript.Regexp")
    .Pattern = "[a-z]\d{9}"
    If .test(ref) Then xtract = .Execute(ref)(0)
End With
End Function
Excel Workbook
AB
1VIFIR - kd :k200010006k200010006
2VIFIR - kd : k200010003k200010003
3VIFIR - kd : a100010043a100010043
Sheet1
Excel 2003
Cell Formulas
RangeFormula
B1=xtract(A1)
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top