How to remove all first characters if all are digits in excel

Viewed 51

I have excel file like this

983 David
75Harshan
0 vivek

I want to remove 983 from excel cell 1, remove 75 from second cell, remove 0 from third cell. How this possible? ie. I want to remove all the prefixing digits from an excel cell.

4 Answers

This formula will retain all of the string in A1 except any leading numeric characters. Copy down as required.

=MID(A1,MATCH(TRUE,ISERROR(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))),0),LEN(A1))

The formula works as is on Excel 365. For earlier versions of Excel you may have to enter it as an array formula (confirm with Ctl+Shift+Enter)

For any version of Excel that supports VBA, consider the following User Defined Function:

Option Explicit

Public Function NoDig(s As String) As String
    Dim i As Long, L As Long, CH As String
    NoDig = s
    L = Len(s)
    For i = 1 To L
        CH = Left(NoDig, 1)
        If CH Like "[0-9]" Then
            NoDig = Mid(NoDig, 2, 9999)
        Else
            Exit Function
        End If
    Next i
End Function

enter image description here

User Defined Functions (UDFs) are very easy to install and use:

  1. ALT-F11 brings up the VBE window
  2. ALT-I ALT-M opens a fresh module
  3. paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the UDF:

  1. bring up the VBE window as above
  2. clear the code out
  3. close the VBE window

To use the UDF from Excel:

=myfunction(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

You could use an array formula like

=IF(A2="","",RIGHT(A2,LEN(A2)-MAX(IF(((CODE(MID(A2,SEQUENCE(LEN(A2)),1))<48)+(CODE(MID(A2,SEQUENCE(LEN(A2)),1))>57)),0,SEQUENCE(LEN(A2))))))

or (more fun) a Lambda RemoveDigits(s) if you have it

=LAMBDA(s,IF(s="","",IF(OR(CODE(LEFT(s))<48,CODE(LEFT(s))>57),s,RemoveDigits(RIGHT(s,LEN(s)-1)))))

Note that these give different results in the last example:

enter image description here

I've replaced the first formula with something more or less equivalent to the lambda.

Use text to columns, sort all data with space. And then use space as delimiters

Related