Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Adding a Little Animation to Your Life
Converting a Range of URLs to Hyperlinks
Making the Formula Bar Persistent
Bryan has a worksheet that has a lot of cells that have some alphabetic characters in them. He is looking for a way to get rid of only those alphabetic characters, no matter where they appear in the cell. For instance, if the cell contains "ABC123," Bryan wants to get rid of "ABC" and have just "123" remaining. Similarly, "A3B2C1" should become "321" and "#45P*%" should become "#45*%".
If you want to simply strip out the characters, in place, then you can do so by selecting the cells you want to affect and then running a macro that examines each cell and deletes the offending characters. There are many ways you could do this; the following macro is a straightforward approach.
Sub CleanText1()
Dim rngCell As Range
Dim intChar As Integer
Dim strCheckString As String
Dim strCheckChar As String
Dim intCheckChar As Integer
Dim strClean As String
For Each rngCell In Selection
strCheckString = rngCell.Value
strClean = ""
For intChar = 1 To Len(strCheckString)
strCheckChar = Mid(strCheckString, intChar, 1)
intCheckChar = Asc(strCheckChar)
Select Case intCheckChar
Case 65 To 90 'upper case chars
'Do nothing
Case 97 To 122 'lower case chars
'Do nothing
Case 128 To 151 'special language chars
'Do nothing
Case 153 To 154 'special language chars
'Do nothing
Case 159 To 165 'special language chars
'Do nothing
Case Else
strClean = strClean & strCheckChar
End Select
Next intChar
rngCell.Value = strClean
Next rngCell
End Sub
The nice thing about this approach to stripping out the characters is that you can easily get rid of other characters by simply modifying what is checked (and what actions are taken) in the Select Case structure.
If you don't want to modify the original cells, a good approach is to put together a user-defined function that will return a "clean" version of a string. This can be achieved by making a few modifications to the previous macro.
Function CleanText2(ByVal sRaw As String) As String
Dim intChar As Integer
Dim strCheckString As String
Dim strCheckChar As String
Dim intCheckChar As Integer
Dim strClean As String
Application.Volatile
strClean = ""
For intChar = 1 To Len(sRaw)
strCheckChar = Mid(sRaw, intChar, 1)
intCheckChar = Asc(strCheckChar)
Select Case intCheckChar
Case 65 To 90 'upper case chars
'Do nothing
Case 97 To 122 'lower case chars
'Do nothing
Case 128 To 151 'special language chars
'Do nothing
Case 153 To 154 'special language chars
'Do nothing
Case 159 To 165 'special language chars
'Do nothing
Case Else
strClean = strClean & strCheckChar
End Select
Next intChar
CleanText2 = strClean
End Function
In order to use the function, you could put a formula such as the following in a cell:
=CleanText2(A1)
The result is that the formula returns a "clean" version of whatever is in cell A1 without disturbing the contents of cell A1.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3219) applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Remove Some Stress at Tax Time! Doing your personal income taxes can be a royal pain. Why not make the process just a bit less stressful with our 101-question checklist. You can prepare for filing your taxes with confidence, knowing you've covered all your bases.