Can't Make This AppleScript Work to Modify an Excel Cell

Thank you for helping me out.

I'm trying to change cell N that contains the subcategory of a music track if it finds the track title I'm looking for in column R.

(The same track titles appear multiple time across the sheet and I'm not allowed to modify the Excel to do a pivot table or to add a macro.)

-- Declare variables
property searchTrackTitle : ""
property newSubCategory : ""

-- Display input box to get search term
display dialog "Enter the track title that you want to search for:" default answer ""
set searchTrackTitle to text returned of result

-- Display input box to get new SubCategory
display dialog "Enter the new SubCategory that you want to use:" default answer ""
set newSubCategory to text returned of result

-- Open Excel and set variables
tell application "Microsoft Excel"
	activate
	set sh to active sheet
	set rng to range "R:R" of sh
end tell

-- Loop through each cell in the range
repeat with cell in rng
	-- Check if the cell value matches the track title
	if value of cell is searchTrackTitle then
		-- Update the SubCategory for the track title
		set (text of cell(("N") as text)) of sh to newSubCategory
	end if
end repeat

Hey Nicolas,

Please provide a zipped Excel workbook that properly demonstrates what you're working with.

-Chris

Hey Nicolas,

I think I've got something working without needing a sample workbook.

I would not use AppleScript's display dialogs for user text entry – I'd use Keyboard Maestro's Prompt for User Input action and pipe that into your AppleScript action.

Here's how to do that:

# NOTE – the AppleScript variables are actual variables.
# The Keyboard Maestro variable NAMES are STRINGS.

set kmInstance to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   # Set a Keyboard Maestro variable:
   set asVarName to getvariable "local_copiedText" instance kmInstance
   # Get a Keyboard Maestro variable
   setvariable "local_KM_VarName" instance kmInstance to dataStr
end tell

Your Microsoft Excel Script:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/16 04:29
# dMod: 2022/12/16 04:29 
# Appl: Microsoft Excel
# Task: Find the Given Text in the Given Column – Insert Text in Cell N of that Row.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Microsoft_Excel, @Find, @Search, @Replace
--------------------------------------------------------

set findText to "ambulance"
set insertText to "Notorious"

tell application "Microsoft Excel"
   tell active sheet of active workbook
      
      set foundRange to find range "R:R" what findText
      
      set rowNumber to first row index of foundRange
      select foundRange
      
      set value of cell ("N" & rowNumber) to insertText
      
   end tell
end tell

--------------------------------------------------------

-Chris

1 Like

Thanks for your help! I get "Microsoft Excel got an error: Parameter error." error "Microsoft Excel got an error: Parameter error." number -50

It doesn't like: find range range "R:R" what findText

Link to the Excel (I had to remove data for other rows for company privacy See rows N and R.xlsx - Google Sheets)

Double-check that the text you are searching for is actually there in column R.

1 Like

JEEZ testing codes before 8am doesn't work well for me! IT WORKS!

Is there a way to make it work for every row that has this track title? Because it only did it for 1 row.

You didn't really make this clear in your initial request.

That's why I wanted an example Excel workbook – so that I could test with real-world data.

Please provide one, and I'll look into it further.

Thank you for your time, here it is Loading Google Sheets Column N and Column R

Okay, I think this fills the bill.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/16 04:29
# dMod: 2022/12/16 12:28
# Appl: Microsoft Excel
# Task: Find the Given Text in the Given Column – Insert Text in Cell N of that Row v2.00.
#     : Find-Text may occur multiple times in the given column
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Microsoft_Excel, @Find, @Search, @Replace
# Vers: 2.00
--------------------------------------------------------

set findText to "Traveling Light"
set replaceText to "Blues, Soul, Rock"

set onceFlag to true

tell application "Microsoft Excel"
   tell active sheet of active workbook
      
      repeat
         
         if onceFlag then
            set foundRange to find range "R:R" what findText search order search next
            copy foundRange to foundRangeOne
            set foundRangeARowIndex to foundRange's first row index
            set onceFlag to false
         else
            set foundRange to find next range "R:R" after foundRange
            if foundRangeOne = foundRange then exit repeat
            set foundRangeBRowIndex to foundRange's first row index
         end if
         
         set rowNumber to first row index of foundRange
         # select foundRange
         set value of cell ("N" & rowNumber) to replaceText
         
      end repeat
      
   end tell
end tell

--------------------------------------------------------
2 Likes

Oh my god...GENIUS, once again!! It works perfectly! Thank you so much!

1 Like