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
 
Thank you very much. Works fine :biggrin:. I started to use the function instead of VBA (I have 2007 and does not seem to work but I'll figure out why, cause I'd learn some). Took a while to figure out how arrays work, but read some and it works now. Trying to figure out how it works. Still do not understand all of it, but I'll continue reading on the net and think and I'll be find. But if you could explain briefly, would also help a lot.

Thanks for the many comments on my thread.

p
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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)
hi sanrv1f!

could I ask you to give a small explanation to the function u suggested using?
I have excel 2007 and it does not work. It has a NAME error. I tried to understand
your code, by searching the web, but did not succeed. It would be a GREAT help if
you could give me some heads up on what each command does. I would be really
really greatful. Thank you.
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</pre>
 
Upvote 0
First thing, you need to place the code in a standard module

Goto VBE (Alt + F11) -> Insert -> Module -> Paste the code


As for the code, the Regular expressions (Reg exp) is a pattern matching tool to identify set of chars in a string,

here [a-z] means any small case alphabet, '\d' means any single digit (0-9), the {9} denotes the occurences of the token (any digit),

when you interpret in simple english, Any small case letter followed by 9 numeric chars

see this for more info on Reg Exp

http://www.aivosto.com/vbtips/regex.html
 
Upvote 0
Maybe this?

Excel Workbook
AB
1VIFIR - kd :k200010006 ABC.k200010006
2VIFIR - kd : k200010003k200010003
3VIFIR - kd : a100010043XY QWa100010043
Extract Code
 
Upvote 0

Forum statistics

Threads
1,215,473
Messages
6,125,020
Members
449,203
Latest member
tungnmqn90

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