How to search in selected area in Numbers

While searching in Numbers, I would like a search in selected area only and not in all the sheets in a file

It searches indiscriminately. This is a problem. I cannot narrow down my search.




MacBook Air (M3, 2024)

Posted on Dec 1, 2024 2:10 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 9, 2024 7:59 AM

I use a regular internet search, not the useless "search" feature of this forum, to find posts here.


If you are saying that when you type a number into the "find" field of the script it tacks a 0 onto the end as you are typing, I have not seen that and have no explanation why it would. It is a text entry field. It does not check if something is a digit or any other character or if the entire string when done is a number or just text. Same with the "replace" entry field.


I have found where the problem is with numbers in cells. When getting the value of a cell, an integral number like 123 is received as 123.0 in the script. The script below uses the formatted value of the cell (i.e., what you see displayed) instead. Note that it will round numbers to the number of decimals currently displayed. The built-in find/replace appears to do the same thing.


I made it so it only changes cells that actually need a change. If nothing in a cell was found/replaced then it leaves it alone. So that rounding stuff I mentioned above will only occur to cells that had something found/replaced.


It ignores cells with formulas and those that are blank. The older version of SGIII's script was this way. I just put that back into it.


It highlights "Next" as the default button in the "find" dialog.


It no longer considers case. ABC is the same as Abc, aBc, and so on.


Copy the script from below and replace the one in the shortcut with it. Let me know if it does all you need it to do.



-- Numbers App Find&Replace in Selection
-- SGIII Author
-- 12/09/2024 Badunit edit 
-- 1) Reincorporate the check for formulas and missing values so those cells are not affected. 
-- 2) Use the formatted value of the cell for the find/replace. Prior to this, integral numbers such 
--  as 123 were being turned into 123.0. Note that this will round any decimal numbers to the number of
--  places being displayed, just as the built-in find/replace does.
-- 3) Reincorporated "next" as default button for the "find" dialog
-- 4) Changed it to ignore case vs consider case
-- 5) Set new values for cells only if they have changed.

set f to display dialog "Find this in selected cells in Numbers " default answer "" with title "Find and Replace Step 1" buttons {"Cancel", "Next"} default button "Next"
set f to text returned of result
if f = "" then display dialog "Did you really mean you want to replace null with something?  This will result in your replacement string being inserted between each pair of letters." buttons {"Cancel", "Yes"} with title "Find and Replace"

display dialog "Replace '" & f & "' with " default answer "" with title "Find and Replace Step 2"
set r to text returned of result

try
	tell application "Numbers" to tell front document to tell active sheet
		tell (first table whose selection range's class is range)
			set sr to selection range
			tell sr to repeat with i from 1 to count cells
				--if not a formula and not blank
				if cell i's formula is missing value and cell i's value is not missing value then
					set oVal to (cell i's formatted value)
					--return oVal
					set nVal to my findReplace(oVal, f, r)
					-- return nVal
					if (cell i's formatted value) ≠ nVal then
						set cell i's value to nVal
					end if
				end if
			end repeat
		end tell
	end tell
on error
	display dialog "Are the cells where you want to do a Find Replace selected?" buttons {"cancel"} with title "Oops!"
end try

--handlers
to findReplace(tt, f, r)
	set oTID to AppleScript's text item delimiters
	ignoring case
		set AppleScript's text item delimiters to f
		set lst to tt's text items
		set AppleScript's text item delimiters to r
		set tt to lst as string
	end ignoring
	set AppleScript's text item delimiters to oTID
	return tt
end findReplace

18 replies
Question marked as Top-ranking reply

Dec 9, 2024 7:59 AM in response to Bomiboll

I use a regular internet search, not the useless "search" feature of this forum, to find posts here.


If you are saying that when you type a number into the "find" field of the script it tacks a 0 onto the end as you are typing, I have not seen that and have no explanation why it would. It is a text entry field. It does not check if something is a digit or any other character or if the entire string when done is a number or just text. Same with the "replace" entry field.


I have found where the problem is with numbers in cells. When getting the value of a cell, an integral number like 123 is received as 123.0 in the script. The script below uses the formatted value of the cell (i.e., what you see displayed) instead. Note that it will round numbers to the number of decimals currently displayed. The built-in find/replace appears to do the same thing.


I made it so it only changes cells that actually need a change. If nothing in a cell was found/replaced then it leaves it alone. So that rounding stuff I mentioned above will only occur to cells that had something found/replaced.


It ignores cells with formulas and those that are blank. The older version of SGIII's script was this way. I just put that back into it.


It highlights "Next" as the default button in the "find" dialog.


It no longer considers case. ABC is the same as Abc, aBc, and so on.


Copy the script from below and replace the one in the shortcut with it. Let me know if it does all you need it to do.



-- Numbers App Find&Replace in Selection
-- SGIII Author
-- 12/09/2024 Badunit edit 
-- 1) Reincorporate the check for formulas and missing values so those cells are not affected. 
-- 2) Use the formatted value of the cell for the find/replace. Prior to this, integral numbers such 
--  as 123 were being turned into 123.0. Note that this will round any decimal numbers to the number of
--  places being displayed, just as the built-in find/replace does.
-- 3) Reincorporated "next" as default button for the "find" dialog
-- 4) Changed it to ignore case vs consider case
-- 5) Set new values for cells only if they have changed.

set f to display dialog "Find this in selected cells in Numbers " default answer "" with title "Find and Replace Step 1" buttons {"Cancel", "Next"} default button "Next"
set f to text returned of result
if f = "" then display dialog "Did you really mean you want to replace null with something?  This will result in your replacement string being inserted between each pair of letters." buttons {"Cancel", "Yes"} with title "Find and Replace"

display dialog "Replace '" & f & "' with " default answer "" with title "Find and Replace Step 2"
set r to text returned of result

try
	tell application "Numbers" to tell front document to tell active sheet
		tell (first table whose selection range's class is range)
			set sr to selection range
			tell sr to repeat with i from 1 to count cells
				--if not a formula and not blank
				if cell i's formula is missing value and cell i's value is not missing value then
					set oVal to (cell i's formatted value)
					--return oVal
					set nVal to my findReplace(oVal, f, r)
					-- return nVal
					if (cell i's formatted value) ≠ nVal then
						set cell i's value to nVal
					end if
				end if
			end repeat
		end tell
	end tell
on error
	display dialog "Are the cells where you want to do a Find Replace selected?" buttons {"cancel"} with title "Oops!"
end try

--handlers
to findReplace(tt, f, r)
	set oTID to AppleScript's text item delimiters
	ignoring case
		set AppleScript's text item delimiters to f
		set lst to tt's text items
		set AppleScript's text item delimiters to r
		set tt to lst as string
	end ignoring
	set AppleScript's text item delimiters to oTID
	return tt
end findReplace

Dec 21, 2024 1:47 AM in response to Badunit

I concocted another method for "find - replace" which works in many situations (not all)

Here is my "algorithm"

1: sort ascending your column

2: choose the first cell to be replaced

3: replace that cell with the text you want

4: drag the cell down the column. All the following cells will be replaced

This is a very fast and easy to remember method


Dec 5, 2024 1:05 PM in response to Bomiboll

It is SGIII's script/shortcut. All I did was post a link to it. It does what you requested -> "search in selected area only". You are right it does not do find&replace. The first script I posted, also written by SGIII, does find&replace all "in a selected area only". If you need that, try again to download and install the first shortcut. What you posted in your screenshot was not it. Neither one highlights cells.

Dec 3, 2024 7:13 PM in response to Bomiboll

I'm not sure what you did because that is not the shortcut. The shortcut is entirely an AppleScript. But this is not what you were asking for anyway. My mistake. It finds and replaces, it won't stop on or highlight the "found" cells. I think it would be possible to edit it to make it stop on found cells and let you move to the next one using a mouse click or something. I don't know if SGIII will get around to that or maybe me or someone else and I don't know when it might happen.

Dec 8, 2024 8:06 AM in response to Bomiboll

The link is supposed to be to a Shortcut app shortcut, not a service. It is run via the Shortcuts app. You can put Shortcut app shortcuts up on the menu bar at the top of the screen for easy access. If the link from the other thread is giving you problems, here is a link to my copy of the shortcut.


EDIT: I'll post a link soon. I have two versions of the shortcut and I the link I provided might install both. I need to get rid of one of them.

Dec 8, 2024 8:36 AM in response to Bomiboll

If you open the Shortcuts app and look at the shortcut, do you see the Applescript that is the heart of this shortcut? What you posted awhile back was not it.


I just realized you are the OP from the other link, which was from a year ago. Did this not work then either?


Prior to the Shortcut app the way to do this was to create a service. SGIII provided a link to a service back then. You probably have it in your services menu from a year ago, not from anything you did recently.


I want to make a change to the shortcut version of this script. If you select empty cells for find&replace it will replace them with "missing value". The older script did not do that. I can't do it right now, though.

Dec 8, 2024 12:56 PM in response to Bomiboll

I believe somewhere in all those posts it said it was case sensitive. I'll look into it. One thing I noticed with numbers is it changes the formatting of whole numbers from "auto" decimal places to 1 decimal place and tacks on a ".0". Is it doing other things as well? It also changes blank cells into "missing value" which is something easily fixable.


The version you installed does find/replace within formulas, too, but that can be dangerous because it can find/replace letters within a function name and break the formula. I'm editing my version back to the original where it skipped over formulas.


Post some specific examples of things that are not working as expected and I can see what I can do. The list should contain the original value, find text, replace text, and final value, and what is wrong with the result.

Dec 8, 2024 9:14 PM in response to Badunit

I can see the Apple Script

set f to display dialog "Find this in selected cells in Numbers " etc

It tacks on a "0" as you write. It also adds "missing value" where the selected cells are empty

I often need to change dot to comma or eliminat comma f ex 4,4 to 4.4 or 44

It then changes other numbers too by adding a zero. So I cannot use it for replacing numbers

You ask if I am the OP from the other link

Yes. It was not working then, but now there is some success.

I wonder how you found that link. My old posts are shown only the last 3 months. THat means that I tend to repeat my topics instead of reviewing them.

Dec 10, 2024 5:17 AM in response to Bomiboll

It would be easier if you told me what actually happened vs saying "did not do the job" and leaving it up to me to figure out what you are seeing. I'm trying to help you here.


It works here. I tried it in a region that uses a dot as the decimal separator and also in one that uses comma, both in English. I tried replacing 22,22 with 2222. I also tried replacing the comma with nothing. Both worked properly. If you got the result 22.22 where the comma was replaced by a dot, the script that actually ran might be the old one that did not use the formatted value of the cell.

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.

How to search in selected area in Numbers

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