Get text from string after last number

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,427
Office Version
  1. 2016
Platform
  1. Windows
Is there a quick way I can extract the text in a string after the last number?

My strings are all letters, then numbers, then letters again - the numbers always fall in the middle of the string, but I need the text at the end, excluding any spaces.

Is it possible please guys?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Formula?
vba?

It would also be good to have a few varied examples of "before" and "after" so we understand exactly what you mean by "excluding any spaces"
 
Upvote 0
Here's a UDF for that:
VBA Code:
Function FindNumber(s As String) As Long
For x = 1 To Len(s)
    If IsNumeric(Mid(s, x, 1)) Then Exit For
Next
FindNumber = Val(Mid(s, x))
End Function

Book3
AB
1abc12345ABC12345
2qwertyjflb12djx12
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=FindNumber(A1)
 
Upvote 0
Scott it seems sharky12345 wants the text after the numbers without spaces. Here's a not so eloquent solution. Dave
VBA Code:
Sub Test()
Dim i As Integer, NewStr As String
'loop backwards through string
For i = Len(Sheets("Sheet1").Range("A" & 1).Value) To 1 Step -1
    'build string with non numbers
    If Asc(Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1)) < 48 Or _
    Asc(Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1)) > 57 Then
    'don't include non print chars
        If Asc(Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1)) < 8 Or _
        Asc(Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1)) > 13 Then
        'don't include blank space
        If Asc(Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1)) <> 32 Then
        'make forward string
        NewStr = Mid(Sheets("Sheet1").Range("A" & 1).Value, i, 1) + NewStr
        End If
    End If
Else
'output result/stop search loop when number found
MsgBox NewStr
Exit For
End If
Next i
End Sub
edit: forgot the code is set to test A1 in sheet1
 
Upvote 0
Is there a quick way I can extract the text in a string after the last number?

My strings are all letters, then numbers, then letters again - the numbers always fall in the middle of the string, but I need the text at the end, excluding any spaces.

Is it possible please guys?
Try this.

VBA Code:
Public Function fncFindLastString(s As String) As String
Dim x As Integer

  For x = 2 To Len(s)
    If Not IsNumeric(Mid(s, x, 1)) And IsNumeric(Mid(s, x - 1, 1)) Then Exit For
  Next
  
  fncFindLastString = Mid(s, x)
  
End Function
 
Upvote 0
Book2
AB
1abc12345ABCABC
2qwertyjflb12djxdjx
Sheet1
Cell Formulas
RangeFormula
B1:B2B1=RIGHT(A1,LEN(A1)-MAX(IF(ISNUMBER(VALUE(MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))),ROW(INDIRECT("1:" & LEN(A1))))))
 
Upvote 0
VBA Code:
Function FindLastText(s As String) As String
For x = Len(s) To 1 Step -1
    If IsNumeric(Mid(s, x, 1)) Then Exit For
Next
FindLastText = Mid(s, x + 1)
End Function
 
Upvote 0
Is there a quick way I can extract the text in a string after the last number?

My strings are all letters, then numbers, then letters again - the numbers always fall in the middle of the string, but I need the text at the end, excluding any spaces.

Is it possible please guys?
Or.

Sample Data.xlsm
ABC
1ddsfsdf5353gffgdgfgffgdgf
2ewew4324erwerewerwerew
3retert543543gfdhhhfgfdhhhf
Temp
Cell Formulas
RangeFormula
C1:C3C1=TRIM(RIGHT(CONCAT(IF(ISNUMBER((MID(A1,ROW(A1:INDIRECT("A"&LEN(A1))),1)*1)),REPT(" ",100),(MID(A1,ROW(A1:INDIRECT("A"&LEN(A1))),1)))),100))


Excel Formula:
=TRIM(RIGHT(CONCAT(IF(ISNUMBER((MID(A3,ROW(A3:INDIRECT("A"&LEN(A3))),1)*1)),REPT(" ",100),(MID(A3,ROW(A3:INDIRECT("A"&LEN(A3))),1)))),100))
 
Upvote 0
Or.

Sample Data.xlsm
ABC
1ddsfsdf5353gffgdgfgffgdgf
2ewew4324erwerewerwerew
3retert543543gfdhhhfgfdhhhf
Temp
Cell Formulas
RangeFormula
C1:C3C1=TRIM(RIGHT(CONCAT(IF(ISNUMBER((MID(A1,ROW(A1:INDIRECT("A"&LEN(A1))),1)*1)),REPT(" ",100),(MID(A1,ROW(A1:INDIRECT("A"&LEN(A1))),1)))),100))


Excel Formula:
=TRIM(RIGHT(CONCAT(IF(ISNUMBER((MID(A3,ROW(A3:INDIRECT("A"&LEN(A3))),1)*1)),REPT(" ",100),(MID(A3,ROW(A3:INDIRECT("A"&LEN(A3))),1)))),100))
I do not believe XL2016 has the CONCAT function in it.
 
Upvote 0

Forum statistics

Threads
1,226,117
Messages
6,189,068
Members
453,524
Latest member
AshJames

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