Extracting if there 8 numbers combinations in string

Ks1102

Well-known Member
Joined
Jan 8, 2008
Messages
689
Dear Experts

Would you help i have code using for extract number if 8 Digi combination .. but something wrong for counts if more the eight ... to extracted .

For example :
String : HAL LOCKER # P2018061101136 / T# 96825809 before act .
the code would extracted 20180611 :( .... Not the 96825809 in string

Thanks very much your help
Rich (BB code):
Public Function GetMy8Digits(cell As Range)
Dim s As String
Dim i As Integer
Dim answer
Dim counter As Integer
s = cell.Value
counter = 0
For i = 1 To Len(s)
    If IsNumeric(Mid(s, i, 1)) = True Then

        answer = answer + Mid(s, i, 1)
        counter = counter + 1

        If counter = 8 Then
            GetMy8Digits = answer
            Exit Function
        End If
     Else
     counter = 0
     answer = ""
    End If
Next i
End Function
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this and let me know if it works...

VBA Code:
Public Function GetMy8Digits(Cell As Range)
    Dim CheckString As Variant
   
    CheckString = Trim(Split(Cell.Value, "T#")(1))
    CheckString = Split(CheckString, " ")(0)
   
    If IsNumeric(CheckString) And Len(CheckString) = 8 Then
        GetMy8Digits = CheckString
    Else
        GetMy8Digits = "0"
    End If
End Function
 
Upvote 0
Hi, here is an alternative you can try.

VBA Code:
Public Function GetMy8Digits(cell As Range)
Dim v As Variant
For Each v In Split(cell.Value, " ")
    If Len(v) = 8 And IsNumeric(v) Then
        GetMy8Digits = v
        Exit Function
    End If
Next v
End Function
 
Upvote 0
Hi, here is an alternative you can try.

VBA Code:
Public Function GetMy8Digits(cell As Range)
Dim v As Variant
For Each v In Split(cell.Value, " ")
    If Len(v) = 8 And IsNumeric(v) Then
        GetMy8Digits = v
        Exit Function
    End If
Next v
End Function


Thank you, reply you after trying.
 
Upvote 0
Try this and let me know if it works...

VBA Code:
Public Function GetMy8Digits(Cell As Range)
    Dim CheckString As Variant
  
    CheckString = Trim(Split(Cell.Value, "T#")(1))
    CheckString = Split(CheckString, " ")(0)
  
    If IsNumeric(CheckString) And Len(CheckString) = 8 Then
        GetMy8Digits = CheckString
    Else
        GetMy8Digits = "0"
    End If
End Function
Dear formR

this code getting error "CheckString = Trim(Split(Cell.Value, "T#")(1))"

But another one work perfectly .. thanks so much
 
Upvote 0
Oops, didn't see the last part of your comment. See the code below to retrieve the first set of numbers instead of the last set of numbers with the above code.

VBA Code:
Public Function GetMy8Digits(cell As Range)
    Dim CheckString As Variant
    CheckString = cell.value
    CheckString = Trim(Split(CheckString, "#")(1))
    CheckString = VAL(Split(CheckString, " ")(0))
  
    If IsNumeric(CheckString) And Len(CheckString) =< 8 Then
        GetMy8Digits = CheckString
    Else
        GetMy8Digits = "0"
    End If
End Function
 
Upvote 0
And one more option
VBA Code:
Function Get8Digits(r As Range)
    With CreateObject("VBScript.RegExp")
        .Pattern = "(# )(\d+)"
        If .test(r.Value) Then
            Get8Digits = .Execute(r.Value)(0).submatches(1)
        End If
    End With
End Function
 
Upvote 0
And one more option
VBA Code:
Function Get8Digits(r As Range)
    With CreateObject("VBScript.RegExp")
        .Pattern = "(# )(\d+)"
        If .test(r.Value) Then
            Get8Digits = .Execute(r.Value)(0).submatches(1)
        End If
    End With
End Function
THANKS , IT'S WORK GREAT ..
 
Upvote 0
Great
So glad I could help
Thank you for the feedback
Be Happy
 
Upvote 0
THANKS , IT'S WORK GREAT ..
So the problem actually had nothing to do with 8 digits but just extract whatever number of digits followed "# "?

For example:
Ks1102.xlsm
AB
1HAL LOCKER # 201 / T# 96825809 before act201
2HAL LOCKER # P2018061101136 / T# 96825809569812356111 before act96825809569812356111
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=Get8Digits(A1)
 
Upvote 0

Forum statistics

Threads
1,214,531
Messages
6,120,073
Members
448,943
Latest member
sharmarick

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