Find a word that contain 6 digit in a cell using formula

oniototo

New Member
Joined
May 10, 2022
Messages
11
Office Version
  1. 2021
Platform
  1. Windows
Example: A1 Cell contain "Psd4 j%65 K56L456%4 Jin k444"
Result after Formula : K56L456%4

is there any formula to find the word that contain 6 digit in a cell?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I don't think it is possible with or perhaps it is simpler to use UDF

Put this custom function in regular module

VBA Code:
Function GetNum(rng As Range) As String

Dim RegEx As Object
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
    .Pattern = ".?\d.?\d.?\d.?\d.?\d.?\d"
    .Global = False
End With
If RegEx.Test(rng.Value) Then GetNum = RegEx.Execute(rng)(0)

End Function

To use
Book1
ABCDEFG
1Psd4 j%65 K56L456%4 Jin k444K56L456%4
2
Sheet1
Cell Formulas
RangeFormula
F1F1=GetNum(A1)
 
Upvote 0
Welcome to the MrExcel board!

I assume that you mean "exactly 6 digits" in the 'word' rather than "6 or more"

If so, you could try the formula in column B.
Alternatively, below is a user-defined function that you could also consider. Its use is shown in columns C & D, where it is used with its optional second argument if you wanted to look for words with a different number of digits

For both of my suggestions, if it is possible that 2 or more 'words' contain the correct number of digits, the first one is returned.

VBA Code:
Function nDigitWord(s As String, Optional NumDigits As Long = 6) As String
  Dim itm As Variant
  Dim i As Long, c As Long
 
  For Each itm In Split(s)
    c = 0
    For i = 1 To Len(itm)
      If IsNumeric(Mid(itm, i, 1)) Then c = c + 1
    Next i
    If c = NumDigits Then
      nDigitWord = itm
      Exit Function
    End If
  Next itm
End Function

oniototo.xlsm
ABCD
1Psd4 j%65 K56L456%4 Jin k444K56L456%4K56L456%4j%65
2K56L456%4 Jin k445K56L456%4K56L456%4 
3K56L456%4 Jin k446 K56L999%4 Jin k446K56L456%4K56L456%4 
4Psd4 j%65 K56L456%4K56L456%4K56L456%4j%65
5Psd4 j%6 K56L456%   
6Psd4 j%65 K56986547L456%  j%65
7j%65 K56986547L456% G678987RG678987RG678987Rj%65
6 digits
Cell Formulas
RangeFormula
B1:B7B1=LET(t,MID(A1,SEQUENCE(LEN(A1)),1),ck,CONCAT(IF(t=" ",t,IF(ISNUMBER(t+0),1,""))),tt,LEFT(ck,FIND(" "&111111&" "," "&ck&" ")),IFERROR(TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",100)),(LEN(tt)-LEN(SUBSTITUTE(tt," ","")))*100+1,100)),""))
C1:C7C1=nDigitWord(A1)
D1:D7D1=nDigitWord(A1,2)


@Zot
Your udf fails if data like row 7 in my sample is possible.
 
  • Like
Reactions: Zot
Upvote 0
@Zot
Your udf fails if data like row 7 in my sample is possible.
For a regular expressions udf, this might be a possibility

VBA Code:
Function GetWord(s As String) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "( |^)(([^ \d]*\d){6}[^ \d]*)(?= |$)"
  If RX.Test(s) Then GetWord = LTrim(RX.Execute(s)(0))
End Function

oniototo.xlsm
AB
1Psd4 j%65 K56L456%4 Jin k444K56L456%4
2K56L456%4 Jin k445K56L456%4
3K56L456%4 Jin k446 K56L999%4 Jin k446K56L456%4
4Psd4 j%65 K56L456%4K56L456%4
5Psd4 j%6 K56L456% 
6Psd4 j%65 K56986547L456% 
7j%65 K56986547L456% G678987RG678987R
6 digits (2)
Cell Formulas
RangeFormula
B1:B7B1=GetWord(A1)
 
  • Like
Reactions: Zot
Upvote 0

Forum statistics

Threads
1,225,209
Messages
6,183,599
Members
453,173
Latest member
Ali4772

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