I would like to search on column H, clear the entire contents of the cells that contain the word “brunch” and replace it with the letter B.
What I have now is:
repeat
set target to (find (range "H:H") what "Brunch" with match case)
clear contents of target
insert into (character 0 of target) string "B"
end repeat
That works fine, but when it’s found and replaced the last “B”, I get an AppleScript error and I’d really like to learn to do this properly.
The issue is your repeat. The error is generated because when it runs out of [b], it cannot perform the [set]. So, use a try block with a repeat exit:
repeat
try
set target to (find (range "H:H") what "Brunch" with match case)
clear contents of target
insert into (character 0 of target) string "B"
on error
exit repeat
end try
end repeat
I could not test this, as I have nothing in Excel to use, so let me know how this works for you.