Question : Remove all non-integers from a cell in Excel

What is the easiest way to remove all non-integers text (<> 0-9) from a cell in Excel?

For example, I have the following text in cell [A1]:
(714) 429-2020 x.6157

and I want to return the following number in cell [B1]:
71442920206157

I have many veriations of the above text, so the answer cannot be specific to its spacing and format.

Answer : Remove all non-integers from a cell in Excel

Another approach is to use a UDF which works just like a function in a formula. To use the UDF below:

   =RemoveNonNumericChars(A1)

Add this code to any general code module:

Public Function RemoveNonNumericChars(ByVal Value As String) As String

' Remove all non-numeric characters from the parameter.
   
    Dim Result As String
    Dim Index As Long

    RemoveNonNumericChars = Value
    Result = ""
    For Index = 1 To Len(Value)
       If Mid(Value, Index, 1) Like "#" Then
          Result = Result + Mid(Value, Index, 1)
       End If
    Next Index
    If Result <> Value Then
       RemoveNonNumericChars = Result
    End If

End Function

To add VBA code to a regular or general module in an Excel workbook, press ALT+F11 to open the VBA development environment (VBE). Select the menu command Insert->Module to create a new VBA module. Paste the code into the document window that appears. Press ALT+F11 to return to the Excel workbook.

Kevin
Random Solutions  
 
programming4us programming4us