Formula To Find Only Instances of 4 digits In A ROW Within Cell And Return It To The Adjacent Cell

CONFUSED_AS_USUAL

Board Regular
Joined
Jul 6, 2017
Messages
59
Example:

[TABLE="width: 260"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]SOURCE[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]#5364[/TD]
[TD="align: right"]5364[/TD]
[/TR]
[TR]
[TD]#5571[/TD]
[TD="align: right"]5571[/TD]
[/TR]
[TR]
[TD]1 P/O#5159/1 WEB SALE[/TD]
[TD="align: right"]5159[/TD]
[/TR]
[TR]
[TD]20 SOLD P/O#5221[/TD]
[TD="align: right"]5221[/TD]
[/TR]
[TR]
[TD]24 SOLD P/O#4968[/TD]
[TD="align: right"]4968[/TD]
[/TR]
[TR]
[TD]24 SOLD P/O#5149[/TD]
[TD="align: right"]5149[/TD]
[/TR]
[TR]
[TD]3 SOLD P/O#5193[/TD]
[TD="align: right"]5193[/TD]
[/TR]
[TR]
[TD]36 SOLD P/O#5322[/TD]
[TD="align: right"]5322[/TD]
[/TR]
[TR]
[TD]6 SOLD P/O#5245[/TD]
[TD="align: right"]5245[/TD]
[/TR]
[TR]
[TD]lee tz qc[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]P/O#4379[/TD]
[TD="align: right"]4379[/TD]
[/TR]
[TR]
[TD]P/O#4409[/TD]
[TD="align: right"]4409[/TD]
[/TR]
[TR]
[TD]P/O#4497[/TD]
[TD="align: right"]4497[/TD]
[/TR]
[TR]
[TD]P/O#4497[/TD]
[TD="align: right"]4497[/TD]
[/TR]
</tbody>[/TABLE]

Thanks you
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,

If the numbers you're looking for are Always preceded by the symbol # and Always 4 digits:

00 HTML Conversions.xlsm
AB
1SOURCERETURN
2#53645364
3#55715571
41 P/O#5159/1 WEB SALE5159
520 SOLD P/O#52215221
624 SOLD P/O#49684968
724 SOLD P/O#51495149
83 SOLD P/O#51935193
936 SOLD P/O#53225322
106 SOLD P/O#52455245
11lee tz qc 
12P/O#43794379
13P/O#44094409
14P/O#44974497
15P/O#44974497
Sheet9
Cell Formulas
RangeFormula
B2:B15B2=IFERROR(MID(A2,FIND("#",A2)+1,4)+0,"")


Formula copied down.
 
Last edited by a moderator:
Upvote 0
Apart from the issue raised about the preceding "#", it is also not clear whether your data could possibly contain a string of more than 4 digits and, if so, what should happen in that case.
Anyway, one way to accomplish this would be to use a user-defined function. I've included two such functions below for you to consider. I have assumed that you want exactly 4 digits rather than 4 or more. I have made the UDFs flexible by allowing an optional 2nd argument if you wanted to search for a different number of digits - for example, as shown in the last row of my sample worksheet.

To implement a UDF..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Code:
Function ExtrDigits(s As String, Optional lNumDigits As Long = 4) As Variant
  Static RX As Object
 
  If RX Is Nothing Then Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(^|\D)(\d{" & lNumDigits & "})(\D|$)"
  ExtrDigits = ""
  If RX.Test(s) Then ExtrDigits = CLng(RX.Execute(s)(0).submatches(1))
End Function


Function GetDigits(s As String, Optional lNumDigits As Long = 4) As Variant
  Dim i As Long
  Dim sLike As String
 
  GetDigits = ""
  s = "x" & s & "x"
  sLike = "[!0-9]" & String(lNumDigits, "#") & "[!0-9]"
  For i = 1 To Len(s) - lNumDigits - 1
    If Mid(s, i, lNumDigits + 2) Like sLike Then
      GetDigits = CLng(Mid(s, i + 1, lNumDigits))
      Exit For
    End If
  Next i
End Function

00 HTML Conversions.xlsm
ABCD
1SOURCE
2#536453645364
3#557155715571
41 P/O#5159/1 WEB SALE51595159
520 SOLD P/O#522152215221
624 SOLD P/O#496849684968
724 SOLD P/O#514951495149
83 SOLD P/O#519351935193
936 SOLD P/O#532253225322
106 SOLD P/O#524552455245
11lee tz qc  
12P/O#437943794379
13P/O#440944094409
14P/O#449744974497
15P/O#449744974497
165632X56325632
17W457645764576
18
19P/O#437984379843798
Sheet3
Cell Formulas
RangeFormula
B2:B17B2=ExtrDigits(A2)
D2:D17D2=GetDigits(A2)
B19B19=ExtrDigits(A19,5)
D19D19=GetDigits(A19,5)
 
Last edited:
Upvote 0
Apart from the issue raised about the preceding "#", it is also not clear whether your data could possibly contain a string of more than 4 digits and, if so, what should happen in that case.
Anyway, one way to accomplish this would be to use a user-defined function. I've included two such functions below for you to consider. I have assumed that you want exactly 4 digits rather than 4 or more. I have made the UDFs flexible by allowing an optional 2nd argument if you wanted to search for a different number of digits - for example, as shown in the last row of my sample worksheet.

To implement a UDF..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)


Gawwwwwd, I love you because
a) you wrote code that does EXACTLY what the OP/I need, and
b) you included help on how to add a UDF to a file!!

Thank you for your awesome response, from someone who found it 2+ years later :)
 
Upvote 0
Gawwwwwd, I love you because
a) you wrote code that does EXACTLY what the OP/I need, and
b) you included help on how to add a UDF to a file!!

Thank you for your awesome response, from someone who found it 2+ years later :)
You're welcome. :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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