Formula to identify cells that have 7 numbers with a string

powercell99

Board Regular
Joined
May 14, 2014
Messages
75
Looking for help, suggestions for a formula that would identify cells that contain a string (text string, like a sentence) but within the sentence there is a grouping of 7 numbers together. ex: cell value: "this item is for john in the mail room, 1234567". the 7 numbers could be at any character position, but they would be grouped together, because the cell is freehand and any number of people would enter the data.

the numbers could be any number or combination of numbers, but there would be 7 of them. There isnt a numerical wildcard character for excel? like: search(" #######", A1, 1)

Any suggestions or recommendations you could think of would be greatly appreciated.

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
not sure if there is a direct way to do it in excel but in vba the logic is easy to write... (the code can probably be improved, i wrote quickly)

G4RyofX.png


Code:
Function ContainsNumbers(text As String) As Boolean
    Dim i As Integer
    Dim numCount As Integer
    For i = 1 To Len(text)
        If IsNumeric(Mid(text, i, 1)) Then [COLOR=#008000]'check each character, count or reset[/COLOR]
            numCount = numCount + 1
        Else
            numCount = 0
        End If
        
        If numCount = 7 Then
            ContainsNumbers = True
            Exit Function
        End If
    Next i
    
    ContainsNumbers = False
End Function
 
Last edited:
Upvote 0
Thanks cerfani! that is really great. i like it alot. how hard would it be to show the character position of the first number instead of True, and FALSE if not found??
 
Upvote 0
Hi,

If there is Only one number string within the sentence,
A formula solution to find the actual Number String, and Position where the Number String starts:


Book1
ABC
1Number String FoundPosition
2this item is for john in the mail room, 1234567123456741
32345678 items are in the mail room23456781
4john took 3456789 items to the mail room345678911
Sheet73
Cell Formulas
RangeFormula
B2=MID(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789")),7)
C2=MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&"0123456789"))


Note: Formula will fail if there are More than one 7 digit number strings within the sentence.
 
Last edited:
Upvote 0
well you can just save the position from the i integer when you start the count (numCount = 1) and reset it when you reset numCount

d9b2ikJ.png


Code:
Function SevenNumbersPosition(text As String) As Integer
    Dim i As Integer, numCount As Integer, position As Integer
    For i = 1 To Len(text)
        If IsNumeric(Mid(text, i, 1)) Then [COLOR=#008000]'check each character, count or reset[/COLOR]
            If numCount = 0 Then
                position = i
            End If
            numCount = numCount + 1
        Else
            position = 0
            numCount = 0
        End If
        
        If numCount = 7 Then
            SevenNumbersPosition = position
            Exit Function
        End If
    Next i
    
    SevenNumbersPosition = 0
End Function
 
Last edited:
Upvote 0
not sure if there is a direct way to do it in excel but in vba the logic is easy to write... (the code can probably be improved, i wrote quickly)

G4RyofX.png


Code:
Function ContainsNumbers(text As String) As Boolean
    Dim i As Integer
    Dim numCount As Integer
    For i = 1 To Len(text)
        If IsNumeric(Mid(text, i, 1)) Then [COLOR=#008000]'check each character, count or reset[/COLOR]
            numCount = numCount + 1
        Else
            numCount = 0
        End If
        
        If numCount = 7 Then
            ContainsNumbers = True
            Exit Function
        End If
    Next i
    
    ContainsNumbers = False
End Function

Here is another, more compact way to write your ContainsNumbers function...
Code:
[table="width: 500"]
[tr]
	[td]Function ContainsNumbers(S As String) As Boolean
  ContainsNumbers = " " & S & " " Like "*[!0-9]#######[!0-9]*"
End Function[/td]
[/tr]
[/table]
 
Upvote 0
well you can just save the position from the i integer when you start the count (numCount = 1) and reset it when you reset numCount

d9b2ikJ.png


Code:
Function SevenNumbersPosition(text As String) As Integer
    Dim i As Integer, numCount As Integer, position As Integer
    For i = 1 To Len(text)
        If IsNumeric(Mid(text, i, 1)) Then [COLOR=#008000]'check each character, count or reset[/COLOR]
            If numCount = 0 Then
                position = i
            End If
            numCount = numCount + 1
        Else
            position = 0
            numCount = 0
        End If
        
        If numCount = 7 Then
            SevenNumbersPosition = position
            Exit Function
        End If
    Next i
    
    SevenNumbersPosition = 0
End Function
And here is another, more compact way to write your SevenNumbersPosition function...
Code:
Function SevenNumbersPosition(ByVal S As String) As Long
  Dim X As Long
  S = " " & S & " "
  For X = 1 To Len(S) - 7
    If Mid(S, X, 9) Like "[!0-9]#######[!0-9]" Then
      SevenNumbersPosition = X
      Exit Function
    End If
  Next
End Function
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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