vba find first set of numbers in a string ignoring any subsequent

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I'm looking for a solution to the following issue please.

I am importing data into excel from a web application where some of the data is in "free text" format.

I am trying to extract the first group of numeric values from a string, so for example cell in cell A1 if the value is something like "Hello, my name is Scott and I live at 123 Maiden Road, I've lived here for the last 23 years" - so in this I would need to see 123 in the adjacent cell, ignoring the "23" part of the string.

I know this could probably be done with a formula, but a vba solution would be better so I can include it with the rest of my project.

Thanks
 
I've just ran this on 10,000 lines of data and haven't come accross any issue yet, but I would like to cover for any eventuality. A UDF is fine now I know how to call it in my sub.

How about this. Using the above functions and in case of having the eventuality mentioned by @Rick "12e3"


Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("=min(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        If n = Len(c) Then
            c.Offset(0, 1).Value = Mid(c, n)
        Else
            For j = n + 1 To Len(c)
                If Mid(c, j, 1) Like "*[!0-9]*" Then
                    c.Offset(0, 1).Value = Mid(c, n, j - n)
                    Exit For
                End If
            Next
        End If
    Next
End Sub
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about this. Using the above functions and in case of having the eventuality mentioned by @Rick "12e3"
Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("=min(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        If n = Len(c) Then
            c.Offset(0, 1).Value = Mid(c, n)
        Else
            For j = n + 1 To Len(c)
                If Mid(c, j, 1) Like "*[!0-9]*" Then
                    c.Offset(0, 1).Value = Mid(c, n, j - n)
                    Exit For
                End If
            Next
        End If
    Next
End Sub
You can simplify your code by substituting the "e" away...
Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("=min(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        c.Offset(, 1) = Val(Replace(Mid(UCase(c.Value), n), "E", "x"))
    Next
End Sub
 
Last edited:
Upvote 0
You can simplify your code by substituting the "e" away...
Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("MIN(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        c.Offset(, 1) = Val(Replace(Mid(UCase(c.Value), n), "E", "x"))
    Next
End Sub
Whoops! I forgot about "D" (does the same thing as "E" in the VB world)...
Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("MIN(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        c.Offset(, 1) = Val(Replace(Replace(Mid(UCase(c.Value), n), "E", "x"), "D", "x"))
    Next
End Sub
 
Last edited:
Upvote 0
You can simplify your code by substituting the "e" away...
Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("=min(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&""0123456789""))")
        c.Offset(, 1) = Val(Replace(Mid(UCase(c.Value), n), "E", "x"))
    Next
End Sub

If the "e" is what could eventually affect, then I guess that's it.
 
Upvote 0
How about:

"some text 12.1 some more"
Result:
12.1

With my last code
Result
12
 
Upvote 0
How about:

"some text 12.1 some more"
Result:
12.1

With my last code
Result
12
The OP will have to tell us if floating point numbers are to be permitted or not. If not, we can replace the dot as well.
 
Last edited:
Upvote 0
The OP will have to tell us if floating point numbers are to be permitted or not. If not, we can replace the dot as well.


Okay, I think that would be the last replacement.
Well, while, another improved version:


Code:
Sub extnum3()
    Dim c As Range, n As Long, j As Long
    For Each c In Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        n = Evaluate("=min(FIND({0,1,2,3,4,5,6,7,8,9}," & c.Address & "&"" 0123456789""))")
        j = n + 1
        Do While Mid(c, j, 1) Like "*[0-9]*"
            j = j + 1
        Loop
        c.Offset(0, 1).Value = Mid(c, n, j - n)
    Next
End Sub
 
Upvote 0
The OP will have to tell us if floating point numbers are to be permitted or not. If not, we can replace the dot as well.

Hi, no floating point numbers, so 12.1 should be 12. Also if possible, is there a way of identifying cells that have multiple sets of numbers within the string? I would still need to output just the first set, but then a way of identifying any anomalies.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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