VBA to extract last number from string

JohnDouglas

Board Regular
Joined
Jan 5, 2005
Messages
239
Any ideas how to extract the last number from these strings???

I guess the VBA would need to right trim. which i can do, but not sure how to then find the final space " " and extract all the digits after it
... any ideas?


example 1: "Page: First | -10 | 51 "

example 2: "Page: First | -10 | 231 232 233 234 235 236 "

Thanks for you help

John
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
1 Find the length of the string using len()
2 set up a loop that will count back from the right using mid() until it finds a space
3 use right() to get everthing to the right of that position.
 
Upvote 0
Something like this.

Code:
Private Sub CommandButton1_Click()
  Dim A As String
  Dim B As Integer
  Dim C As Integer
  Dim D As String
  A = Range("A1").Value
  B = Len(A)
  For Position = B To 1 Step -1
    C = Mid(A, Position, 1)
    If C = " " Then
      D = Right(A, B - Position)
      Range("A2").Value = D
      Exit Sub
    End If
  Next Position
End Sub

Just place a control button on your worksheet, put your value in cell A1 and press the button. The code will search out the final space in the value and report everything to its right in cell A2
 
Upvote 0
That's strange.

This code was copied directly out of a VBA code window in which it was working perfectly
 
Upvote 0
Oops!

Just tested it again and found that i dimmed C wrong. it should have been a string. try this.

Code:
Private Sub CommandButton1_Click()
  Dim A As String
  Dim B As Integer
  Dim C As String
  Dim D As String
  A = Range("A1").Value
  B = Len(A)
  For Position = B To 1 Step -1
    C = Mid(A, Position, 1)
    If C = " " Then
      D = Right(A, B - Position)
      Range("A2").Value = D
      Exit Sub
    End If
  Next Position
End Sub
 
Upvote 0
fantastic, it still wasn't working but then i realised a trim was needed.

thanks for your help with this, i'm learning more every day!

cheers

john

final code
Code:
Private Sub CommandButton1_Click()
  Dim A As String
  Dim B As Integer
  Dim C As String
  Dim D As String
  A = Range("A1").Value
  A = Trim(A)
  B = Len(A)
  For Position = B To 1 Step -1
    C = Mid(A, Position, 1)
    MsgBox C
    If C = " " Then
      D = Right(A, B - Position)
         Range("A2").Value = D
      Exit Sub
    End If
  Next Position
End Sub
 
Upvote 0
Hi

UDF
use in cell like
=john(A1)

Code:
Function john(txt As String) As Long
Dim x
x = Split(Trim(txt), Chr(32))
john = Val(x(UBound(x)))
End Function
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,453
Members
449,161
Latest member
NHOJ

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