Getting numbers from a string/cell

SQUIDD

Well-known Member
Joined
Jan 2, 2009
Messages
2,104
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi all

So I need to get just numbers from a cell or string.

I can obviously do a replace A with “”. And b,c,d and so on. On 26 lines.

Anyway I can write it shorter.

Like column(1).replace((“a,b,c”),””)

I’m just trying to get the numbers from a string. Using vba. Not formula.

Thanks.

Dave.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
The following checks A3 and returns the numbers from it.

VBA Code:
    s = Range("A3")
'
    For i = 1 To Len(s)
        If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then
            AllNumbersString = AllNumbersString + Mid(s, i, 1)
        End If
    Next

MsgBox AllNumbersString
 
Upvote 0
This would do a range:

VBA Code:
    Dim cell                As Range
    Dim AllNumbersString    As String
'
    For Each cell In Range("A1:A10")
        For i = 1 To Len(cell.Value)
            If Mid(cell.Value, i, 1) >= "0" And Mid(cell.Value, i, 1) <= "9" Then
                AllNumbersString = AllNumbersString + Mid(cell.Value, i, 1)
            End If
        Next
'
        cell.Value = AllNumbersString
        AllNumbersString = ""
    Next
 
Upvote 0
Hi Johnny.

Thanks for the help.
I have implemented this into my code.

Perfect ?

Dave.
 
Upvote 0

Forum statistics

Threads
1,203,435
Messages
6,055,362
Members
444,781
Latest member
rishivar

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