Excel formula to extract the number in a text

LAY2022

New Member
Joined
Jun 19, 2024
Messages
8
Office Version
  1. 365
HI Masters,

I need help in getting the invoice number in a cell with a text string.

Book1
ABC
1SUPPLIERDETAILSINVOICE#
2ABC COINV#: 66042262 - 522.00 INV#: 66042335 - 960.00 Tools66042262 66402335
3ABC COINV#: 66038272 Pvc 66038272
4ABC COINV#: 6602869366028693
Sheet1


Thanks,
Lay
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
One way.

EXCEL
ABCD
1SUPPLIERDETAILSINVOICE#Custom Function
2ABC COINV#: 66042262 - 522.00 INV#: 66042335 - 960.00 Tools66042262 6640233566042262 66042335
3ABC COINV#: 66038272 Pvc 6603827266038272
4ABC COINV#: 660286936602869366028693
Sheet1
Cell Formulas
RangeFormula
D2:D4D2=rx(B2)


VBA Code:
Function RX(s As String)
Dim SD As Object:   Set SD = CreateObject("Scripting.Dictionary")
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "INV#: (\d+)"
    Set matches = .Execute(s)
    For Each m In matches
        SD.Add m.submatches(0), Null
    Next m
End With

For Each itm In SD
    RX = RX & itm & Chr(10)
Next itm

RX = Left(RX, Len(RX) - 1)

End Function
 
Upvote 0
Another way:

Excel Formula:
=MAP(B2:B4,LAMBDA(array,
LET(
a,TRANSPOSE(TEXTSPLIT(SUBSTITUTE(array,CHAR(10)," ")," ")),
b,a="INV#:",
c,SEQUENCE(ROWS(a))*b,
d,FILTER(c,c<>0)+1,
TEXTJOIN(CHAR(10),,INDEX(a,d,0)))))
 
Upvote 0
I tried this: =TEXTJOIN(CHAR(10),TRUE,TOROW(LEFT(TEXTAFTER(TEXTSPLIT(B2,CHAR(10)),": "),8),2))

with Wrap Text in the column.
 
Upvote 0
Solution
Another way:

Excel Formula:
=MAP(B2:B4,LAMBDA(array,
LET(
a,TRANSPOSE(TEXTSPLIT(SUBSTITUTE(array,CHAR(10)," ")," ")),
b,a="INV#:",
c,SEQUENCE(ROWS(a))*b,
d,FILTER(c,c<>0)+1,
TEXTJOIN(CHAR(10),,INDEX(a,d,0)))))

Thank you, I will try this.
 
Upvote 0
This is more simple. Thank you so much.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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