how to fetch a reference number from a cell and post in another

skf786

Board Regular
Joined
Sep 26, 2010
Messages
156
hello,

my data is in column A. Each cell consists of a sentence which contains a 10 digit reference number as well somewhere. Format of the reference is ACXXXXXXXXXX where the last 8 digits are numbers. I would like the column B to return the reference number ACXXXXXXXXXX. Please note that the reference can be anywhere in the cell i.e. beginning, middle or end and at times even joint with other text without space.

thank you for your help

Khalid
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If the reference number is the only case of capitalized AC in the string then this should work.
If not then need for information.

Book1
AB
10the ref number is AC12345678.AC12345678
11ref# AC12345687 for this row.AC12345687
12the account is 5678 ref AC87654321AC87654321
13AC65432178 is the numberAC65432178
Sheet3
Cell Formulas
RangeFormula
B10:B13B10=MID(A10,FIND("AC",A10),10)


Note this uses find since it is case sensitive so it will ignore other cases of ac like in account. the reference will always need to start with a capitalized AC.
 
Upvote 0
Thank you Scott. This works well. Also, is it possible to pick only where the last 8 digits are numbers only, not alphabets?
 
Upvote 0
If you want to look at the number I think a UDF would be best. As you can see from below the IF statement can only handle one case of ac (in the string that is not the ref number). You would need to nest more if statements to account for more cases of ac in the string. The substitute is case sensitive so it will only work if the non ref number that has AC in caps. It can be wrapped in iserror so it does not return an value error.

Book1
ABC
10the ref number is AC12345678.AC12345678AC12345678
11ref# AC12345687 for this row.AC12345687AC12345687
12the account is 5678 ref AC87654321AC87654321AC87654321
13AC65432178 is the numberAC65432178AC65432178
14the account is 876 for acer ac98765412acer ac987ac98765412
15125 account go fad fasa AC32158746AC32158746AC32158746
16this does not have a ref number#VALUE!not found
Sheet3
Cell Formulas
RangeFormula
B10:B16B10=IF(I10,MID(A10,G10,10),MID(SUBSTITUTE(A10,"ac","||",1),SEARCH("ac",SUBSTITUTE(A10,"ac","||",1)),10))
C10:C16C10=refnum(A10)


The UDF will return not found if there is no ref number. that can be changed to what you want.
VBA Code:
Function refnum(rng As Range)

Dim ccount As Long
ccount = Len(rng)
For x = 1 To ccount
    If Mid(rng, x, 1) >= "0" And Mid(rng, x, 1) <= "9" And x > 2 Then
        If UCase(Mid(rng, x - 2, 2)) = "AC" Then
            refnum = Mid(rng, x - 2, 10)
            Exit For
        End If
    End If
Next x
If refnum = Empty Then refnum = "not found"
End Function
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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