Excel UDF: To exctract All Numbers, Special Characters and Alphabets from a String

.

Sometimes you require to Extract All Numbers and Characters from a mixed String. I have written a UDF (User Defined Function) to extract them.

  1. UDF Function to Extract Numbers from a String

  2. UDF Function to Extract Special Characters from a String

  3. UDF Function to Extract Alphabets from a String

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:

Extract Numbers, Strings, Special Characters

Extract Numbers, Strings, Special Characters



Function ExtractNumber(Cell As Range)
	Dim ResultNum As Long
	Dim ResultSpecialChar, ResultAlpha As String
	Dim InputString As String
	InputString = Cell.Value
	For i = 1 To Len(InputString)
		If IsNumeric(Mid(InputString, i, 1)) = True Then
			Result = Result & Mid(InputString, i, 1)
			
		ElseIf (Asc(Mid(InputString, i, 1)) <= 65 Or Asc(Mid(InputString, i, 1)) > 90) _
			And ((Asc(Mid(InputString, i, 1)) < 97 Or Asc(Mid(InputString, i, 1)) >= 122)) Then
			
			ResultSpecialChar = ResultSpecialChar & Mid(InputString, i, 1)
		Else
			ResultAlpha = ResultAlpha & Mid(InputString, i, 1)
		End If
	Next
	ExtractNumber = "Alphabets are:-  " & ResultAlpha & "  **  Numbers are: " & Result & "  **  Special Chars:" & ResultSpecialChar
End Function


If you want only Numbers to be extracted from the String then Use the below code in Module



Function ExtractNumber(Cell As Range)
    Dim ResultNum As Long
    Dim InputString As String
    InputString = Cell.Value
    For i = 1 To Len(InputString)
        If IsNumeric(Mid(InputString, i, 1)) = True Then
            ResultNum = ResultNum & Mid(InputString, i, 1)
        End If
    Next
    ExtractNumber = ResultNum
End Function

If you want only Special Characters to be extracted from the String then Use the below code in Module



Function ExtractSpecialChar(Cell As Range)
	Dim ResultSpecialChar As String
	Dim InputString As String
	InputString = Cell.Value
	For i = 1 To Len(InputString)
		If (Asc(Mid(InputString, i, 1)) <= 65 Or Asc(Mid(InputString, i, 1)) > 90) _
		And ((Asc(Mid(InputString, i, 1)) < 97 Or Asc(Mid(InputString, i, 1)) >= 122) _
		And IsNumeric(Mid(InputString, i, 1)) = False) Then
		ResultSpecialChar = ResultSpecialChar & Mid(InputString, i, 1)
	End If
Next
ExtractSpecialChar = ResultSpecialChar
End Function

If you want only Alhabets to be extracted from the String then Use the below code in Module







Function ExtractAlphabets(Cell As Range)
	Dim ResultAlphabet As String
	Dim InputString As String
	InputString = Cell.Value
	For i = 1 To Len(InputString)
		If (Asc(Mid(InputString, i, 1)) >= 65 And Asc(Mid(InputString, i, 1)) <= 90) _
		Or ((Asc(Mid(InputString, i, 1)) >= 97 And Asc(Mid(InputString, i, 1)) <= 122)) Then
		ResultAlphabet = ResultAlphabet & Mid(InputString, i, 1)
	End If
Next
ExtractAlphabets = ResultAlphabet
End Function

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

How to Copy content from Word using VBA

As many of us want to deal with Microsoft Word Document from Excel Macro/VBA. I am going to write few articles about Word from Excel Macro. This is the first article which opens a Word Document and read the whole content of that Word Document and put it in the Active...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

1 Comment

  1. Shagun

    Thanks. Very Helpful !

    Reply

Trackbacks/Pingbacks

  1. Learn Excel Macro How to create User Defined Function in Excel - [...] UDF – Is File Open? UDF to Convert Currency to Words UDF to Count Words in Cell UDF to…
  2. How to create User Defined Functions in Excel - Welcome to LearnExcelMacro.com - […]    UDF to Extract Numbers from a cell […]
  3. Calculate MOD of Large Numbers in Excel - Let's excel in Excel - […] WORKSHEET FUNCTION : AGE CALCULATIONEXCEL UDF: TO EXTRACT ALL NUMBERS, SPECIAL CHARACTERS AND ALPHABETS FROM A STRINGUSER DEFINED FUNCTION…

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest