I'm trying to extract the exact sequence after a term in a text and just returns the number.
I need the whole sequence to return, with numbers and characters between the number.
For example:

I'm using this vba code:
Function ExtraiSeq(rng As Range)
Dim i As Integer, str As String, CharPos As Long
str = rng.Value
CharPos = InStr(1, str, "SEQ", vbTextCompare) + 2
If CharPos > 2 Then
For i = CharPos To Len(rng)
Select Case Asc(Mid(rng.Value, i, 1))
Case 48 To 57
ExtraiSeq = Trim(ExtraiSeq & Mid(rng.Value, i, 1))
End Select
Next i
Else: ExtraiSeq = ""
End If
End Function
try,
Option Explicit
Function justDigits(str As String)
Dim i As Long
i = InStr(1, str, "seq ", vbTextCompare)
If CBool(i) Then
For i = i + 4 To Len(str)
Select Case Asc(Mid(str, i, 1))
Case 46 To 57
justDigits = justDigits & Mid(str, i, 1)
Case else
Exit For
End Select
Next i
End If
End Function

Updated answer:
Preserving all the characters between the first number found after "seq" and the last number found you can use:
Function getNumbersFromString(strIn As String, strTerm As String) As String
Step1 = Trim(Mid(strIn, InStr(1, strIn, strTerm) + 3, Len(strIn)))
For startIndex = 1 To Len(Step1)
If IsNumeric(Mid(Step1, startIndex, 1)) Then Exit For
Next
For endIndex = Len(Step1) To 1 Step -1
If IsNumeric(Mid(Step1, endIndex, 1)) Then Exit For
Next
getNumbersFromString = Mid(Step1, startIndex, endIndex - startIndex + 1)
End Function
Original wrong answer:
I think something like the following would do the trick for you:
Function getNumbersFromString(strIn As String) As String
For Each char In Split(StrConv(strIn, 64), vbNullChar)
If IsNumeric(char) Then getNumbersFromString = getNumbersFromString & char
Next char
End Function
That splits the incoming string into an array where each element is a character. Then it just tests it to see if it's numeric. If it is, it appends to the output.
You can use it as a UDF so in your cell you can just put:
=getNumbersFromString(A1)
If you only want numbers after your "seq" term you can do something like:
Function getNumbersFromStringAfterTerm(strIn As String, strTerm As String) As String
For Each char In Split(StrConv(Mid(strIn, InStr(1, strIn, strTerm), Len(strIn)), 64), vbNullChar)
If IsNumeric(char) Then getNumbersFromStringAfterTerm = getNumbersFromStringAfterTerm & char
Next char
End Function
Which would then be usable like:
=getNumbersFromStringAfterTerm(A1, "seq")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With