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
[TABLE="width: 134"]
<tbody>[TR]
[TD]
abc125
[/TD]
[TD="align: right"]125
[/TD]
[/TR]
[TR]
[TD]bcd121
[/TD]
[TD="align: right"]121
[/TD]
[/TR]
[TR]
[TD]111aaa
[/TD]
[TD="align: right"]111
[/TD]
[/TR]
</tbody>[/TABLE]

While the code works as:

[TABLE="width: 134"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]abc125
[/TD]
[TD="align: right"]125
[/TD]
[/TR]
[TR]
[TD]bcd121
[/TD]
[TD="align: right"]125121[/TD]
[/TR]
[TR]
[TD]111aaa
[/TD]
[TD="align: right"]125121111[/TD]
[/TR]
</tbody>[/TABLE]

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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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