{"id":12120,"date":"2012-01-19T10:39:53","date_gmt":"2012-01-19T10:39:53","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1265"},"modified":"2022-08-06T20:21:22","modified_gmt":"2022-08-06T20:21:22","slug":"udf-to-extract-numbers-special-chars-alphabets","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/01\/udf-to-extract-numbers-special-chars-alphabets\/","title":{"rendered":"Excel UDF: To exctract All Numbers, Special Characters and Alphabets from a String"},"content":{"rendered":"
Sometimes you require to Extract All Numbers and Characters from a mixed String. I have written a UDF (User Defined Function) to extract them.<\/p>\n
Add the below Code in any of your Regular modules of the Excel VBA Editor. Now go to your Excel Workbook and Type this formula. It will extract All the Special Characters, Numbers, and Alphabets separately as shown below: <\/p>\n
<\/p>\n
Extract Numbers, Strings, Special Characters<\/p><\/div>\n
<\/p>\n
\r\n\r\nFunction ExtractNumber(Cell As Range)\r\n\tDim ResultNum As Long\r\n\tDim ResultSpecialChar, ResultAlpha As String\r\n\tDim InputString As String\r\n\tInputString = Cell.Value\r\n\tFor i = 1 To Len(InputString)\r\n\t\tIf IsNumeric(Mid(InputString, i, 1)) = True Then\r\n\t\t\tResult = Result & Mid(InputString, i, 1)\r\n\t\t\t\r\n\t\tElseIf (Asc(Mid(InputString, i, 1)) <= 65 Or Asc(Mid(InputString, i, 1)) > 90) _\r\n\t\t\tAnd ((Asc(Mid(InputString, i, 1)) < 97 Or Asc(Mid(InputString, i, 1)) >= 122)) Then\r\n\t\t\t\r\n\t\t\tResultSpecialChar = ResultSpecialChar & Mid(InputString, i, 1)\r\n\t\tElse\r\n\t\t\tResultAlpha = ResultAlpha & Mid(InputString, i, 1)\r\n\t\tEnd If\r\n\tNext\r\n\tExtractNumber = \"Alphabets are:- \" & ResultAlpha & \" ** Numbers are: \" & Result & \" ** Special Chars:\" & ResultSpecialChar\r\nEnd Function\r\n\r\n\r\n<\/code><\/pre>\n<\/a><\/p>\nIf you want only Numbers to be extracted from the String then Use the below code in Module<\/h3>\n\r\n\r\nFunction ExtractNumber(Cell As Range)\r\n Dim ResultNum As Long\r\n Dim InputString As String\r\n InputString = Cell.Value\r\n For i = 1 To Len(InputString)\r\n If IsNumeric(Mid(InputString, i, 1)) = True Then\r\n ResultNum = ResultNum & Mid(InputString, i, 1)\r\n End If\r\n Next\r\n ExtractNumber = ResultNum\r\nEnd Function\r\n<\/code><\/pre>\n