Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Remove Alpha Special Characters From Range Using Excel VBA

This document describes two methods to remove alpha and special characters from a range of cells in Excel using VBA: 1. A procedure that loops through each cell in a range, extracts only numeric characters, and overwrites the cell value. 2. A user-defined function that takes a single cell as input, loops through its characters, concatenates only numeric ones, and returns the cleaned string. Examples and code are provided for both methods along with instructions for implementation.

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Remove Alpha Special Characters From Range Using Excel VBA

This document describes two methods to remove alpha and special characters from a range of cells in Excel using VBA: 1. A procedure that loops through each cell in a range, extracts only numeric characters, and overwrites the cell value. 2. A user-defined function that takes a single cell as input, loops through its characters, concatenates only numeric ones, and returns the cleaned string. Examples and code are provided for both methods along with instructions for implementation.

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Remove Alpha Special characters from Range using Excel VBA

VBA Remove Alpha Special characters from Range helps to remove unwanted characters and numbers from a
selected Range . For example, user has to enter the non alphanumeric characters in a Cell or range. We need to
do data validation whether user enters non alphanumeric or not.If user enters also, we need remove those alpha
characters from cells using excel VBA.

 Solution
 Example Cases
o Remove Alpha Special characters – Using Procedure
 Code
 Output
 Instructions
o Remove Alpha Special characters – Using Function
 Code
 Output
 Instructions

VBA Remove Alpha Special characters from Range – Solution(s):


First of all we need to check whether entered data in a cell is alpha characters or not. If it is not with using
following code we can remove those alpha characters from cell. Following are the two different methods.

Remove Alpha Special Characters using Excel VBA – Example


Cases:
Here are the examples to show you how to remove the alpha and special characters from data. Following are the
two different methods, the first one is a procedure to remove alpha and special characters from a specific range.
And the second one is the user defined function to remove the alpha and special characters from Cells.

Remove Alpha Special characters – Using Procedure

Following is the example to Remove Alpha Special characters from cells in a selected Range using Excel VBA.

Code:

'Remove All Alpha and Special characters from cell or Range using VBA code
Sub Remove_AlphaCharacters_From_Cell_Or_Range()

'Variable Declaration
Dim iCnt As Integer
Dim IpData As Range, DataRange As Range
Dim sData As String, sTmp As String
'Create Object for Selected Range
On Error Resume Next
Set DataRange = Sheets("Sheet1").Range("A2:A10")

If DataRange.Count < 1 Then


MsgBox ("Please select range which contains data in cells"), vbInformation
Exit Sub
End If

'Runnning for loop to check all available cells


For Each IpData In DataRange
sTmp = ""
For iCnt = 1 To Len(IpData.Value)
If Mid(IpData.Value, iCnt, 1) Like "
[0-9]" Then
sData = Mid(IpData.Value, iCnt, 1)
Else
sData = ""
End If
sTmp = sTmp & sData
Next iCnt

'Reassign Final Numeric characters


IpData.Value = sTmp
Next IpData

End Sub

Output:
Instructions:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a new module from Insert menu
4. Copy the above code and Paste in the code window
5. Goto Sheet1 and Enter some data in Range(“A2:A10”) with AlphaNumeric and Special Characters
6. Press F5 to remove Alpha and Special Characters from the data
7. Now you should see only numeric data at A2:A10

Remove Alpha Special characters – Using Function

Following is the one more example to Remove Alpha Special characters from cells in a selected Range using
user defined function with Excel VBA.

Code:
'Remove All Alpha and Special characters from cell
Function Remove_AlphaSpecialChar(DataCell As Range) As String

'Variable Declaration
Dim iCnt As Integer
Dim IpData As Range
Dim sData As String, sTmp As String

If DataCell.Count <> 1 Then


MsgBox ("Please select Single Cell"), vbInformation
Exit Function
End If

'Runnning for loop to check all available Characters in a cell


For iCnt = 1 To Len(DataCell.Text)
If Mid(DataCell.Text, iCnt, 1) Like "[0-9]" Then
sData = sData & Mid(DataCell.Text, iCnt, 1)
End If
Next iCnt

Remove_AlphaSpecialChar = sData
End Function

Output:

Here is the screenshot for showing User defined function to remove Alpha and Special Characters from the
cells.
Instructions:

1. Open an excel workbook


2. Press Alt+F11 to open VBA Editor
3. Insert a new module from Insert menu
4. Copy the above function and Paste in the code window
5. Goto Sheet1 and Enter some data in Range(“A2:A10”) with AlphaNumeric and Special Characters
6. In Column B, you can enter the formula (=Remove_AlphaSpecialChar(A2)) to remove the Alpha and
Special Characters
7. Now you should see only numeric data at B2, You can drag the formula upto B10.

You might also like