vba extract only numbers in next row

dushyantgupta111

New Member
Joined
Sep 16, 2015
Messages
4
Hi,

I have been trying to extract only numbers in the next row.

actual result as follow:

The result I want is

abc125
125
bcd121
121
111aaa
111

<tbody>
</tbody>

While the code works as:

abc125
125
bcd121
125121
111aaa
125121111

<colgroup><col><col></colgroup><tbody>
</tbody>

Code:
Sub onlynumm()
Dim onlynum As String
Dim valuee As String

For Each cell In Selection
    Dim res As String
    Dim i As Integer
    valuee = cell.Value
    For i = 1 To Len(valuee)
        If IsNumeric(Mid(valuee, i, 1)) = True Then
            res = res + Mid(valuee, i, 1)
        End If
    Next
onlynum = res
cell.Offset(0, 1).Value = onlynum
Next
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Sub onlynumm()
Dim onlynum As String
Dim valuee As String


For Each cell In Selection
    Dim res As String
    Dim i As Integer
    valuee = cell.Value
    For i = 1 To Len(valuee)
        If IsNumeric(Mid(valuee, i, 1)) = True Then
            res = res + Mid(valuee, i, 1)
        End If
    Next
onlynum = res
cell.Offset(0, 1).Value = onlynum
[B]res = Empty[/B]
Next
End Sub

you just need to clear the res variable. Then it should work just fine.
 
Upvote 0
Assuming your numbers are either at the beginning or end of the text (as you examples show), then this macro should also work...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetNumbers()
  Dim cell As Range, Txt As String
  For Each cell In Selection
    Txt = cell.Value
    If Len(Txt) Then
      If Txt Like "#*" Then
        cell.Offset(, 1).Value = Val(Txt)
      Else
        cell.Offset(, 1).Value = Mid(Txt, InStr(Txt, StrReverse(Val(StrReverse(Txt)))))
      End If
    End If
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Code:
Sub onlynumm()
Dim onlynum As String
Dim valuee As String


For Each cell In Selection
    Dim res As String
    Dim i As Integer
    valuee = cell.Value
    For i = 1 To Len(valuee)
        If IsNumeric(Mid(valuee, i, 1)) = True Then
            res = res + Mid(valuee, i, 1)
        End If
    Next
onlynum = res
cell.Offset(0, 1).Value = onlynum
[B]res = Empty[/B]
Next
End Sub

you just need to clear the res variable. Then it should work just fine.

Thanks friel300, it worked. :)
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,677
Members
449,248
Latest member
wayneho98

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