Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1
Default how do i clean out all non-letter characters from an excel sheet

I have an excel sheet and I need to clean out all non-letter
characters and keep only a-z, how do I do that?

  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 8,856
Default how do i clean out all non-letter characters from an excel sheet

You could do it with a user-defined function (UDF) which examines each
character in the cell and only allows A-Z and a-z. Unfortunately, I
don't have enough time to put one together now, but maybe someone else
can.

Pete

On Jun 28, 11:56 pm, jgarbis wrote:
I have an excel sheet and I need to clean out all non-letter
characters and keep only a-z, how do I do that?



  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default how do i clean out all non-letter characters from an excel sheet

This macro will delete all but A-Z in cells with constants.

What do you want done with formulas? Convert to values and strip also?

If so, make revision as noted at code lines below.

Public Sub StripAllButAZs()
''strips out everything except letters
Dim myRange As Range
Dim cell As Range
Dim myStr As String
Dim i As Integer

With Application
.ScreenUpdating = False
.Calculation = xlManual
End With
On Error Resume Next
Set myRange = Range(ActiveCell.Address & "," & Selection.Address) _
.SpecialCells(xlCellTypeConstants)

' Set myRange = Range(ActiveCell.Address & "," & Selection.Address)
' to include formula cells

If myRange Is Nothing Then Exit Sub
If Not myRange Is Nothing Then
For Each cell In myRange
myStr = cell.text
For i = 1 To Len(myStr)
If (Asc(UCase(Mid(myStr, i, 1))) < 65) Or _
(Asc(UCase(Mid(myStr, i, 1))) 90) Then
myStr = Left(myStr, i - 1) & " " & Mid(myStr, i + 1)
End If
Next i
cell.Value = Application.Trim(myStr)
Next cell
End If
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub


Gord Dibben MS Excel MVP

On Thu, 28 Jun 2007 22:56:46 -0000, jgarbis wrote:

I have an excel sheet and I need to clean out all non-letter
characters and keep only a-z, how do I do that?


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,231
Default how do i clean out all non-letter characters from an excel sheet

"Gord Dibben" <gorddibbATshawDOTca wrote...
This macro will delete all but A-Z in cells with constants.

What do you want done with formulas? Convert to values and strip also?

If so, make revision as noted at code lines below.

Public Sub StripAllButAZs()

....
On Error Resume Next
Set myRange = Range(ActiveCell.Address & "," & Selection.Address) _
.SpecialCells(xlCellTypeConstants)

....
If myRange Is Nothing Then Exit Sub


Or you could make error trap branch to a label at the end of the procedure,
which is rather clearer in what's intended.


If Not myRange Is Nothing Then


This is pointless given the preceding If statement.


For Each cell In myRange
myStr = cell.text
For i = 1 To Len(myStr)
If (Asc(UCase(Mid(myStr, i, 1))) < 65) Or _
(Asc(UCase(Mid(myStr, i, 1))) 90) Then
myStr = Left(myStr, i - 1) & " " & Mid(myStr, i + 1)
End If
Next i
cell.Value = Application.Trim(myStr)
Next cell
End If


For Excel 2000 and later, it'd be more efficient to use

Dim c As String * 1
:
For i = 1 To Len(myStr)
c = Mid$(myStr, i, 1)
If c Like "[!A-Za-z]" Then myStr = Replace(myStr, c, "", i)
Next i


With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub


But how do you know calculation had been set to automatic to begin with?
Better to store the calculation setting then restore it at the end. And this
is another good reason to use On Error to branch to a label near the end BUT
ABOVE this final with block. Either that or put your first With block
*AFTER* your .SpecialCells call. You code called with no constant cells in
the selected range could turn calculation from automatic to manual
mysteriously.


  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,231
Default how do i clean out all non-letter characters from an excel sheet

"Harlan Grove" wrote...
....
For Excel 2000 and later, it'd be more efficient to use

Dim c As String * 1
:
For i = 1 To Len(myStr)
c = Mid$(myStr, i, 1)
If c Like "[!A-Za-z]" Then myStr = Replace(myStr, c, "", i)
Next i

....

More efficient still,

If myStr Like "*[!A-Za-z]*" Then
For i = 1 To Len(myStr)
c = Mid$(myStr, i, 1)
If c Like "[!A-Za-z]" Then myStr = Replace(myStr, c, "", i)
Next i
End If




  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,202
Default how do i clean out all non-letter characters from an excel sheet

More efficient still,

If myStr Like "*[!A-Za-z]*" Then
For i = 1 To Len(myStr)
c = Mid$(myStr, i, 1)
If c Like "[!A-Za-z]" Then myStr = Replace(myStr, c, "", i)
Next i
End If


I believe repeated executions of Replace might not be as efficient as
multiple "string stuffing" followed by a single execution of the Replace
function...

If myStr Like "*[!A-Za-z]*" Then
For i = 1 To Len(myStr)
If Mid$(myStr, i, 1) Like "[!A-Za-z]" Then Mid$(myStr, i, 1) = " "
Next
myStr = Replace(myStr, " ", "")
End If

where I chose to use a blank space as the string stuffing character (but any
non-alpha character would do).

Rick

  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
CLR CLR is offline
external usenet poster
 
Posts: 1,998
Default how do i clean out all non-letter characters from an excel sheet

ASAP Utilities, a free add-in available at www.asap-utilities.com has a
feature that will do that nicely......

Vaya ocn Dios,
Chuck, CABGx3



"jgarbis" wrote:

I have an excel sheet and I need to clean out all non-letter
characters and keep only a-z, how do I do that?


  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 733
Default how do i clean out all non-letter characters from an excel sheet

"Rick Rothstein \(MVP - VB\)" wrote...
....
I believe repeated executions of Replace might not be as efficient
as multiple "string stuffing" followed by a single execution of
the Replace function...

If myStr Like "*[!A-Za-z]*" Then
For i = 1 To Len(myStr)
If Mid$(myStr, i, 1) Like "[!A-Za-z]" Then _
Mid$(myStr, i, 1) = " "
Next
myStr = Replace(myStr, " ", "")
End If

where I chose to use a blank space as the string stuffing
character (but any non-alpha character would do).


Depends on the frequency of particular nonalpha characters. If the
nonalphas were random with few if any multiple instances in any cell,
then your approach may be faster. But if there were multiple instances
of particular characters, getting rid of all of them (and shortening
the string in the process) could be faster.

Also, you don't need to insert " " for instances of " " in mystr, so
make the second Like pattern "[!A-Za-z ]".

  #9   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,202
Default how do i clean out all non-letter characters from an excel sheet

Depends on the frequency of particular nonalpha characters. If the
nonalphas were random with few if any multiple instances in any cell,
then your approach may be faster. But if there were multiple instances
of particular characters, getting rid of all of them (and shortening
the string in the process) could be faster.


There is nothing like knowing the data you are working with when designing
an algorithm.

Also, you don't need to insert " " for instances of " " in mystr, so
make the second Like pattern "[!A-Za-z ]".


LOL... Good catch... It always surprises me how easy it is to overlook the
obvious.

Rick

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clean non printable characters and replace with space rtremblay Excel Worksheet Functions 8 December 1st 06 11:10 PM
help wanted to clean up a data sheet jvoortman Excel Discussion (Misc queries) 9 October 24th 05 03:38 PM
How to rename Excel Sheet with more characters? Billy316 Excel Discussion (Misc queries) 4 July 20th 05 05:11 AM
clean characters bookworm Excel Discussion (Misc queries) 1 June 26th 05 04:31 PM
search in the entire sheet for different letter and cal. Peter FS Excel Worksheet Functions 7 June 18th 05 11:59 PM


All times are GMT +1. The time now is 03:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"