Remove all text/non-number characters

Odd question, but is there a way to remove all non-number characters from a column of cells? For example, if I have:

6dfasfads
12Randomletters.

Is there a way to get rid of the letters?

<Edited by Host>

MacBook, Mac OS X (10.6.2)

Posted on Jul 8, 2010 5:35 PM

Reply
9 replies

Jul 9, 2010 1:42 AM in response to Porsche_Fender__

Porsche+Fender wrote:
Odd question, but is there a way to remove all non-number characters from a column of cells? For example, if I have:
6dfasfads
12Randomletters.
Is there a way to get rid of the letters?


For those specific examples, and others following the same pattern (described below), yes.

User uploaded file

Formulas used on the Conversion table and the Main table are shown as they appear in A2 (Conversion table) and C2 (Main table). After entering the formulas, Fill down the rest of the column, then (Conversion table only) Fill Right across all columns.

=IFERROR(VALUE(MID(Main::$B2,1,COLUMN())),"") returns the first n characters of the string in B2 of the main table, with n determined by the column where the formula is placed. It then attempts to translate the returned character string to a number. If successful, that number is returned to the cell; if there's an error (as there will be if the string includes non-numeric characters (with a few exceptions), or if MID() tries to return a string longer that the one being analyzed), the formula returns the empty string ("").

Limitations:
The number must start with the first character of the string, and cannot be interrupted by characters other than the decimal separator, the thousands separator or e (upper or lower case). Some other characters may be possible as well.

As shown, the formula examines only the first nine characters of the original string. For larger numbers, add more columns to the Conversion table and fill the formula into those columns. Note that the maximum precision for numbers in Numbers is 15 places.

=MAX(Conversion::A2:I2) returns the largest number in the range of values calculated by the formula above.

Regards,
Barry

PS: Details regarding the functions used are available in the iWork Formulas and Functions User Guide. See the Help menu in Numbers to download your copy.
B

Jul 9, 2010 2:11 AM in response to KOENIG Yvan

Here is a script doing the trick with no extraneous table or even column.

--

Barry's response is a clever one. I'm a bit bored to miss this track.
--[SCRIPT cleaner]
(*
Enregistrer le script en tant que Script : cleaner.scpt
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.
Sélectionner le groupe de cellules dont le contenu doit être nettoyé
Aller au menu Scripts , choisir Numbers puis choisir cleaner
Les cellules dont le contenu renferme des caractères non-numériques seront débarassées de ceux-ci.
--=====
L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
--=====
Save the script as a Script: cleaner.scpt
Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.
Select the range of cells whose content must be cleaned.
Go to the Scripts Menu, choose Numbers, then choose "cleaner"
The cells whose content embed non numerical characters will drop them.
--=====
The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
--=====
Yvan KOENIG (VALLAURIS, France)
2010/07/09
*)
--=====
property alloweds : "0123456789.,-"
on run
set {dName, sname, tname, rname, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
tell application "Numbers" to tell document dName to tell sheet sname to tell table tname
repeat with c from colNum1 to colNum2
tell column c
repeat with r from rowNum1 to rowNum2
set maybe to value of cell r
try
maybe * 1
on error (*
The cell doesn't contain a true number so it must be cleaned
*)
set clean to {}
repeat with k in maybe
set k to k as text
if k is in alloweds then copy k to end of clean
end repeat
set value of cell r to my recolle(clean, "")
end try
end repeat -- r
end tell -- column c
end repeat -- c
end tell --Numbers
end run
--=====
(*
set {rowNum1, colNum1, rowNum2, colNum2} to my getCellsAddresses(dname,s_name,t_name,arange)
*)
on getCellsAddresses(d_Name, s_Name, t_Name, r_Name)
local two_Names, row_Num1, col_Num1, row_Num2, col_Num2
tell application "Numbers"
set d_Name to name of document d_Name (* useful if we passed a number *)
tell document d_Name
set s_Name to name of sheet s_Name (* useful if we passed a number *)
tell sheet s_Name
set t_Name to name of table t_Name (* useful if we passed a number *)
end tell -- sheet
end tell -- document
end tell -- Numbers
if r_Name contains ":" then
set two_Names to my decoupe(r_Name, ":")
set {row_Num1, col_Num1} to my decipher(d_Name, s_Name, t_Name, item 1 of two_Names)
if item 2 of two_Names = item 1 of two_Names then
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
else
set {row_Num2, col_Num2} to my decipher(d_Name, s_Name, t_Name, item 2 of two_Names)
end if
else
set {row_Num1, col_Num1} to my decipher(d_Name, s_Name, t_Name, r_Name)
set {row_Num2, col_Num2} to {row_Num1, col_Num1}
end if -- r_Name contains…
return {row_Num1, col_Num1, row_Num2, col_Num2}
end getCellsAddresses
--=====
(*
set { dName, sName, tName, rname, rowNum1, colNum1, rowNum2, colNum2} to my getSelParams()
*)
on getSelParams()
local r_Name, t_Name, s_Name, d_Name
set {d_Name, s_Name, t_Name, r_Name} to my getSelection()

if r_Name is missing value then
if my parleAnglais() then
error "No selected cells"
else
error "Il n'y a pas de cellule sélectionnée !"
end if
end if

return {d_Name, s_Name, t_Name, r_Name} & my getCellsAddresses(d_Name, s_Name, t_Name, r_Name)
end getSelParams
--=====
(*
set {rowNumber, columnNumber} to my decipher(docName,sheetName,tableName,cellRef)
apply to named row or named column !
*)
on decipher(d, s, t, n)
tell application "Numbers" to tell document d to tell sheet s to tell table t to ¬
return {address of row of cell n, address of column of cell n}
end decipher
--=====
(*
set { d_Name, s_Name, t_Name, r_Name} to my getSelection()
*)
on getSelection()
local _, theRange, theTable, theSheet, theDoc, errMsg, errNum

tell application "Numbers" to tell document 1
repeat with i from 1 to the count of sheets
tell sheet i
set x to the count of tables
if x > 0 then
repeat with y from 1 to x
try
(selection range of table y) as text
on error errMsg number errNum
set {_, theRange, _, theTable, _, theSheet, _, theDoc} to my decoupe(errMsg, quote)
return {theDoc, theSheet, theTable, theRange}
end try
end repeat -- y
end if -- x>0
end tell -- sheet
end repeat -- i
end tell -- document
return {missing value, missing value, missing value, missing value}
end getSelection
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
on decoupe(t, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
--=====
on recolle(l, d)
local oTIDs, l
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
--=====
--[/SCRIPT]
--


Yvan KOENIG (VALLAURIS, France) vendredi 9 juillet 2010 11:11:17

Jul 11, 2010 6:54 PM in response to Barry

Thanks, for the help.

Yvan, thanks for your solution, but as I am not experienced with Scripts, I will use Barry's solution.

Barry I like your solution and I think I understand it, I use Excel at work, but I am new to Numbers and for whatever reason, it is not working. I have two sheets appropriately named (Main and Conversion like in your example).On the "Conversion" sheet, nothing is showing up and on the "Main" sheet, there is a red error triangle...sorry I can't really think of any more details, bur do you know what is going wrong? I copy-pasted the formulas you put in, the only change was to change B2 to D2, where the cell I want to modify is.

Also, how do you get the top labels (ie letters for columns and numbers for rows) to not disappear when the table is not selected?

Thanks a lot for your help!

p.s. where do I find info on this in the excel menu, I looked, but could not find it....

Thanks again!

Jul 11, 2010 9:45 PM in response to Porsche_Fender__

Porsche+Fender wrote:
I have two sheets appropriately named (Main and Conversion like in your example).On the "Conversion" sheet, nothing is showing up and on the "Main" sheet, there is a red error triangle...sorry I can't really think of any more details, bur do you know what is going wrong? I copy-pasted the formulas you put in, the only change was to change B2 to D2, where the cell I want to modify is.


A single click on an error triangle will open a small alert telling what the error is. On the main sheet, the only errors I'd expect would be a Syntax error (if there's an error in writing the formula) or an invalid reference error if the formula references cells that do not exist.

One of the problems with using formulas including IFERROR is that it acts as an error trap, providing a result in place of the error triangle and its included error message. That can hide what's going on in the core formula. Here's the existing formula as it should appear in column A of your Conversion table:

=IFERROR(VALUE(MID(Main::$D2,1,COLUMN())),"")

With this formula, and a test value 123abc in D2 of the mail table, you should see 1, 12, and 123 in the first three columns of the Conversion table, "empty" cells in the rest of the columns, and 123 in the Main table cell containing the Max() formula.

To see any errors being generated remove the IFERRORportion:

=VALUE(MID(Main::$D2,1,COLUMN()))

With this formula (filled right for the rest of the row), you should see 1, 12, and 123 in the first three columns of the Conversion table, error triangles in the rest of the columns, and and error triangle in the Main table cell containing the Max() formula. The error message in Conversion::Column D and those to the right is "VALUE requires...and found..." This is an expected error, and the reason for the IFERROR() function in the formula. The error message on the Main table should be 'Cell "Conversion::D2" contains an error.'

Errors in any other cells (with that test value used) are unexpected, and knowing the exact error message would hole in determining the cause of the error.
Also, how do you get the top labels (ie letters for columns and numbers for rows) to not disappear when the table is not selected?


You don't. The column and row reference tabs appear only while one or more cells in the table are selected. If you need column and row labels, add a (header) row and (header) column, then enter whatever labels you need.


p.s. where do I find info on this in the excel menu, I looked, but could not find it....


There's no "Excel" menu in Numbers. If you're asking about MS Excel, that would be a question for an MS Excel forum. An internet search for "mactopia excel" (without the quotes) will put you on a pretty direct track to the MS Office and MS Excel 'newsgroups.'

Regards,
Barry

Jul 12, 2010 2:23 AM in response to Porsche_Fender__

Porsche+Fender wrote:
Thanks, for the help.

Yvan, thanks for your solution, but as I am not experienced with Scripts, I will use Barry's solution.


Using a script is as simple that triggering a menu item ** you are free to do as you want.

The interest of a script for this kind of cleaning task is that it doesn't add formulas in the table.
More formulas means smaller speed.

Yvan KOENIG (VALLAURIS, France) lundi 12 juillet 2010 11:22:59

Jul 13, 2010 7:24 PM in response to Barry

Barry wrote:
Porsche+Fender wrote:


p.s. where do I find info on this in the excel menu, I looked, but could not find it....


There's no "Excel" menu in Numbers. If you're asking about MS Excel, that would be a question for an MS Excel forum. An internet search for "mactopia excel" (without the quotes) will put you on a pretty direct track to the MS Office and MS Excel 'newsgroups.'

Regards,
Barry


Sorry, momentary lapse of reason there, I was trying to ask about this:

Barry wrote:


PS: Details regarding the functions used are available in the iWork Formulas and Functions User Guide. See the Help menu in Numbers to download your copy.
B


And I couldn't find it in the Numbers Help menu.

Thanks for your help, I really appreciate it.

KOENIG Yvan wrote:
Porsche+Fender wrote:
Thanks, for the help.

Yvan, thanks for your solution, but as I am not experienced with Scripts, I will use Barry's solution.


Using a script is as simple that triggering a menu item ** you are free to do as you want.

The interest of a script for this kind of cleaning task is that it doesn't add formulas in the table.
More formulas means smaller speed.

Yvan KOENIG (VALLAURIS, France) lundi 12 juillet 2010 11:22:59


That makes sense that a script that a script would reduce the time that something would take to run. I just don't have experience with them, though I would like to learn one day.

I will now see if I can get it to work...

Message was edited by: Porsche+Fender

Jul 13, 2010 11:39 PM in response to Porsche_Fender__

Porsche+Fender wrote:
Barry wrote:


PS: Details regarding the functions used are available in the iWork Formulas and Functions User Guide. See the Help menu in Numbers to download your copy.
B


And I couldn't find it in the Numbers Help menu.


Standard layout for the Menu bar in a Mac application is:

 Application File Edit (then various menus specific to the application) Window Help

Some applications (including Numbers) have one or more menus between the Window menu and the Help menu.

Here's the Numbers '09 Help menu, with the two User Guide menu items marked:

User uploaded file


Regarding:
That makes sense that a script that a script would reduce the time that something would take to run. I just don't have experience with them, though I would like to learn one day.


The advantage that Yvan was pointing out is that until you use it a script just sits there attracting no attention fro the computer at all. A formula, on the other hand, is very active and demanding of attention, especially in an application like Numbers, where the designers have chosen to have calculations updated every time there's a change in any cell. On a larger table (with lots of formulas) that process gobbles up a noticeable chunk of processor time. Putting some functions into scripts removes some formulas from the table, and saves the time those formulas use, letting you get on with data entry more quickly.

As for 'experience,' well I didn't have much of that when I first drove a car with an automatic transmission (my lessons and early driving experience had all been on vehicles with standard transmissions). But there wasn't much to learn: Select Drive, press on the accelerator (try to remember to not step on the clutch when you stop). I couldn't build an automatic transmission then, and I still can't (without a great deal of referring to manuals, and probably much gnashing of teeth).

Scripts are somewhat similar. Using them is pretty simple—Go to the Scripts menu, Select the script. Building them is less simple. I can't do it (without a great deal of referring to manuals, and definitely much gnashing of teeth). But it's no more necessary to know how to build a script in order to use one than it is to know how to build an automatic transmission in order to use one of those.

(Using a script isn't quite as simple as that description, but—speaking from experience as a driving instructor—neither is using an automatic transmission. 😉 )

Regards,
Barry

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Remove all text/non-number characters

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.