Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Please Help. compare two excel files. Can't get the find command to work

Hello, I'm new to applescript. I'm trying to write a script to compare column B of one excel file to column B of the another excel file.


I will have to had a repeat loop to proceed down the column but I'm stuck on the find command.


Below is some of the code I have so far. The applescript error message is "Microsoft Excel got an error: The object you are trying to access does not exist. It highlights the find column."


setpatientFiletochoose filewith prompt"Please select the patient Excel file:"

setpatientNametonameof(info forpatientFile)

tellapplication"Microsoft Excel"

openpatientFile

setthepatientFiletoworkbookpatientName

endtell


display dialog"Is your patient SNP data in column B of the "&" "&patientName&" "&"excel file?"buttons{"No","Yes"}default button2

ifthebutton returnedoftheresultis"No"then

display dialog"Please put your data in column B"buttons{"Cancel"}default button1

ifthebutton returnedoftheresultis"Cancel"then

endif


else

display dialog"Is column E empty? The results of this program will be stored in column E of the"&" "&patientName&" "&"excel file."buttons{"No","Yes"}default button2


ifthebutton returnedoftheresultis"No"then


display dialog"Please put your data in empty column E"buttons{"Cancel"}default button1


ifthebutton returnedoftheresultis"Cancel"then


endif

else


setrelativeFiletochoose filewith prompt"Please select the relative Excel file:"


setrelativeNametonameof(info forrelativeFile)


tellapplication"Microsoft Excel"


openrelativeFile


settherelativeFiletoworkbookrelativeName


endtell

endif

endif


display dialog"Is your relative SNP data in column B of the"&" "&relativeName&" "&"excel file?"buttons{"No","Yes"}default button2

ifthebutton returnedoftheresultis"No"then

display dialog"Please put your data in column B"buttons{"Cancel"}default button1

ifthebutton returnedoftheresultis"Cancel"then

endif

else

display dialog"Does column C contain the result data you would like copied from"&" "&relativeName&" "&"excel file?"buttons{"No","Yes"}default button2

ifthebutton returnedoftheresultis"No"then


display dialog"Please put the data you would like copied in column C."buttons{"Cancel"}default button1


ifthebutton returnedoftheresultis"Cancel"then


endif

else


tellapplication"Microsoft Excel"


activate objectworkbookpatientName


copyvalueofcell"A1"tocellPatientFile


find(range"B:B"ofworkbookrelativeName)whatcellPatientFile


ifcellPatientFileistruethen


setboldoffont objectof cellPatientFiletotrue


endif


endtell

endif

endif



Does any one have any hints towards solving this problem? Thank you for any help.

Posted on Jul 10, 2012 2:06 PM

Reply
Question marked as Best reply

Posted on Jul 12, 2012 6:20 PM

I'm not so familiar with AppleScript & excel.


I'm trying to write a script to compare column B of one excel file to column B of the another excel file.

Not sure why you need the find.


For a quick & not so elegant solution, I'd read in a cell into an appliscript variable. We the cell from the other excel sheet into another variable. Do the comparison in Applescript.


You seem OK in applescript, so maximize you applescript coding.


Here is some code to print out variables.


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles


For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

end run




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


In the future:

-- try to reduce the code to what statements are causing the problem before posting

-- The number of display dialog statements creates a hassle when trying to run the script. I didn't.

-- create a dropbox account. Place some example excel spreadsheets we may use.


Robert

9 replies
Question marked as Best reply

Jul 12, 2012 6:20 PM in response to taffners

I'm not so familiar with AppleScript & excel.


I'm trying to write a script to compare column B of one excel file to column B of the another excel file.

Not sure why you need the find.


For a quick & not so elegant solution, I'd read in a cell into an appliscript variable. We the cell from the other excel sheet into another variable. Do the comparison in Applescript.


You seem OK in applescript, so maximize you applescript coding.


Here is some code to print out variables.


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles


For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run


*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set desktopPath to (path to desktop) as string

log "desktopPath = " & desktopPath

end run




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


In the future:

-- try to reduce the code to what statements are causing the problem before posting

-- The number of display dialog statements creates a hassle when trying to run the script. I didn't.

-- create a dropbox account. Place some example excel spreadsheets we may use.


Robert

Jul 13, 2012 9:05 AM in response to taffners

Here are the books that I use when writing an applescript. Do not know of a good source for AppleScript to Excel.
Do not know why your post didn't draw more interest.

AppleScript

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition the book


AppleScript Language Guide pdf download the pdf file


Intro to applescript with sending an email
http://mac.appstorm.net/how-to/applescript/the-ultimate-beginners-guide-to-apple script/

Jul 13, 2012 10:50 AM in response to taffners

Hi,


1- The find command return a range (not a boolean), or it return an error if nothing was found.

You must use a try block.



2- The find command doesn't work as in the application, you must specify the sheet, otherwise it's the active sheet.

To search in all sheet, you must use a loop.



3- cellPatientFile is a variable wich contains a text, you can't set bold this variable, but it's possible on a variable wich contains a range object.

example :

set cellPatient to range "A1"

setboldoffont objectof cellPatienttotrue

.



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

set patientFile to choose file with prompt "Please select the patient Excel file:"

tell application "Microsoft Excel"

set thepatientWBook to open workbook workbook file name (patientFile as string)

set patientName to name of thepatientWBook

end tell


display dialog "Is your patient SNP data in column B of the " & " " & patientName & " " & "excel file?" buttons {"No", "Yes"} default button 2

if the button returned of the result is "No" then

display dialog "Please put your data in column B" buttons {"Cancel"} default button 1

if the button returned of the result is "Cancel" then

end if


else

display dialog "Is column E empty? The results of this program will be stored in column E of the" & " " & patientName & " " & "excel file." buttons {"No", "Yes"} default button 2

if the button returned of the result is "No" then

display dialog "Please put your data in empty column E" buttons {"Cancel"} default button 1

if the button returned of the result is "Cancel" then

end if

else

set relativeFile to choose file with prompt "Please select the relative Excel file:"

tell application "Microsoft Excel"

set therelativeWBook to open workbook workbook file name (relativeFile as string)

set relativeName to name of therelativeWBook

end tell

end if

end if


display dialog "Is your relative SNP data in column B of the" & " " & relativeName & " " & "excel file?" buttons {"No", "Yes"} default button 2

if the button returned of the result is "No" then

display dialog "Please put your data in column B" buttons {"Cancel"} default button 1

if the button returned of the result is "Cancel" then

end if

else

display dialog "Does column C contain the result data you would like copied from" & " " & relativeName & " " & "excel file?" buttons {"No", "Yes"} default button 2

if the button returned of the result is "No" then

display dialog "Please put the data you would like copied in column C." buttons {"Cancel"} default button 1

if the button returned of the result is "Cancel" then

end if

else

tell application "Microsoft Excel"

activate

activate object thepatientWBook

copy value of cell "A1" to cellPatientFile

try

find range "B:B" of sheet 1 of therelativeWBook what cellPatientFile -- find in first sheet of therelativeWBook

set bold of font object of cell "A1" to true -- cell in thepatientWBook

end try

end tell

end if

end if

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

Jul 13, 2012 12:53 PM in response to Jacques Rioux

Thank you everyone for your help. I attached my code. It works now but kinda slow. Does anyone have any hints on making it faster. Sorry this is my first program ever so I probably went about something completely backwards.



(*

My program SNP Compare was writen for bioinformatics. It compares a patient file to a relatives file. This program takes a column in excel file1 and sees if it is in a column in excel file2. If the answer is found it will copy a field in excel file2 in the same row the answer was found in.


Example data:

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

File one:

Column of data column to paste answer

10

29

7

2

105

102

110

111

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

File two:

Column of data The answer to be copied to file one

29 found row 2

10 found row 3

7 found row 4

2 found row 5

205 found row 6

202 found row 7

210 found row 8

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

File 1 after my program

Column of data Paste answer here

10 found row 3

29 found row 2

7 found row 4

2 found row 5

105 no

102 no

110 no

111 no

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

*)

--opens File1. Names it patientName

setpatientFiletochoose filewith prompt"Please select the patient Excel file:"

setpatientNametonameof(info forpatientFile)

tellapplication"Microsoft Excel"

openpatientFile

setthepatientFiletoworkbookpatientName

endtell



--This Dialog assigns where the data is stored for file1

display dialog"What Column is your patient SNP data of the "&" "&patientName&" "&"excel file?"default answer""buttons{"submit"}default button1

setPatientSNPColto(text returnedoftheresult)


--This Dialog assigns what row your data starts at for file1

display dialog"What row does your patient SNP data start in the "&" "&patientName&" "&"excel file?"default answer""buttons{"submit"}default button1

setpatientSNPRowto(text returnedoftheresult)

setpatientStartCelltoPatientSNPCol&patientSNPRow


--This Dialog assigns the last row of data for file1

display dialog"What row is the last row of you patient SNP data start of the "&" "&patientName&" "&"excel file?"default answer""buttons{"submit"}default button1

setpatientLastRowto(text returnedoftheresult)

setpatientLastCelltoPatientSNPCol&patientLastRow


--This Dialog assigns the location of the data copied from file2

display dialog"What column would you like the results of this program stored in of the"&" "&patientName&" "&"excel file."default answer""buttons{"submit"}default button1

setresultsLocationto(text returnedoftheresult)


--opens File2. Names it relativeName

setrelativeFiletochoose filewith prompt"Please select the relative Excel file:"

setrelativeNametonameof(info forrelativeFile)

tellapplication"Microsoft Excel"

openrelativeFile

settherelativeFiletoworkbookrelativeName

endtell


--This dialog assigns the column of data in file2

display dialog"What Column is your relative SNP data of the "&" "&relativeName&" "&"excel file?"default answer""buttons{"submit"}default button1

setrelativeSNPColto(text returnedoftheresult)


--This dialog assigns the column of data begining copied to excel file1.

display dialog"What Column contains the data you would like copied to the "&" "&patientName&" "&"excel file in column"&" "&resultsLocation&" "&"?"default answer""buttons{"submit"}default button1

setcopyAnswerto(text returnedoftheresult)


--This repeat loop makes the way down the column

repeatwiththisRowfrompatientSNPRowtopatientLastRow


tellapplication"Microsoft Excel"


activate objectworkbookpatientName




settheRangetoPatientSNPCol&thisRow&":"&PatientSNPCol&thisRow


settheValueto(getvalueofrangetheRangeaslist)


settheAnswertoresultsLocation&thisRow&":"&resultsLocation&thisRow



setrelativeSearchRangeto(relativeSNPCol&":"&relativeSNPCol)


copyvalueofcelltheRangetonumberOfCellToSearch




setsearchTermtonumberOfCellToSearch


activate objectworkbookrelativeName


setsearchRangetogetrangerelativeSearchRangeofactive sheet



setfoundSNPCellAddressto""


setfoundCellsto{}


try


setfoundSNPInRelativeto(findsearchRangewhatsearchTerm)


setfoundSNPCellAddresstoget addressfoundSNPInRelative



endtry


--If data is not found a no will be placed on file1


iffoundSNPCellAddress=""then


tellapplication"Microsoft Excel"


activate objectworkbookpatientName


setvalueofcelltheAnswerto"no"


endtell


else


--If data is found the value of data in an assigned column will be copied to a column in file1


tellapplication"Microsoft Excel"


activate objectworkbookpatientName


settheStrtofoundSNPCellAddress


settidtoAppleScript'stext item delimiters


setAppleScript'stext item delimitersto"$"


settheSubStrto(text item3oftheStr)astext


setAppleScript'stext item delimiterstotid



setcopyResulttocopyAnswer&theSubStr&":"&copyAnswer&theSubStr


activate objectworkbookrelativeName



copyvalueofcellcopyResulttocellCopy


activate objectworkbookpatientName


setvalueofcelltheAnswertocellCopy


endtell



endif

endtell

endrepeat

Jul 14, 2012 10:25 AM in response to taffners

this is my first program ever so I probably went about something completely backwards.

Your doing really well. You are natural programmer.

Thank you everyone for your help. I attached my code. It works now but kinda slow. Does anyone have any hints on making it faster.


I'd suggest creating a new post with the script & the question. There's some way of turning off screen refresh. I'll have to look around.


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


Presto. I found this article.

http://lists.apple.com/archives/applescript-users/2011/Apr/msg00129.html


Look for this line.

set screen updating to false

Robert

Jul 14, 2012 10:51 AM in response to rccharles

Your doing really well. You are natural programmer.

Thank you. Well I guess I wrote 1 program before but all it did was open a url on chrome.





set screen updating to false


I will try to look into this set screen updating to false. This looks like a good recommendation. it would be a lot better program if the two excel files do not continue to flash between each other.

Jul 15, 2012 3:22 PM in response to taffners

Hi,

taffners wrote:


Thank you everyone for your help. I attached my code. It works now but kinda slow. Does anyone have any hints on making it faster.


Important : when you use the find command, it's preferable to use the properties ( look in and look at).

Example : findsearchRangewhatsearchTermlook invalueslook atwhole


Otherwise, it will search according to the criteria of your last find.

Example, if you uncheck 'Find entire cells only', and after that you run the script,

find column 2 what 10.0 --> match 110.0 or 1110.0 or 310.0, .... , because it look at part.


---

set screen updating to false : Yes it improves the speed.


Here are my tests (no duplicate values in the Excel column ) on old computer (Powermac G5) .

set screen updating to false without any activate object command

2000 rows --> 3.7 minutes, your script : 11.5 minutes

4000 rows --> 7.5 minutes, your script : 23 minutes

8000 rows --> 15 minutes, your script : 46 minutes



But, it's faster to search through a list without using the Excel find command, no need to set the screen updating, You can continue working on the workbook or another workbook, this will not disturb the script.

Tests on my script :

2000 rows --> 4 seconds

4000 rows --> 21 seconds

8000 rows --> 1.3 minutes

17000 rows --> 6 minutes


Here is the script :

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

main()

display dialog "Done"

onmain()

script o

property patientSNP_list : {}

property relativeSNP_list : {}

property resultsList : {}

property copyAnswerList : {}


onfindLookAtWhole(value) -- case sensitive

set {low, high} to {1, count my relativeSNP_list}

considering case

repeat while lowhigh

set mid_ to (low + high) div 2

if value is in items low thru mid_ of my relativeSNP_list then

set high to mid_

else

set low to mid_ + 1

end if

end repeat

end considering

if (item low of my relativeSNP_list is value) then

set end of my resultsList to item low of my copyAnswerList

else

set end of my resultsList to {"no"}

end if

endfindLookAtWhole

endscript


--opens File1. Names it patientName

set patientFile to choose file with prompt "Please select the patient Excel file:"

setpatientNametonameof (info forpatientFile)

tell application "Microsoft Excel"

open patientFile

setthepatientFiletoworkbookpatientName

setpatientSheettosheet (nameofactive sheet) ofthepatientFile

end tell



--This Dialog assigns where the data is stored for file1

display dialog "What Column is your patient SNP data of the " & " " & patientName & " " & "excel file?" default answer "" buttons {"submit"} default button 1

setPatientSNPColto (text returnedoftheresult)


--This Dialog assigns what row your data starts at for file1

display dialog "What row does your patient SNP data start in the " & " " & patientName & " " & "excel file?" default answer "" buttons {"submit"} default button 1

setpatientSNPRowto (text returnedoftheresult)

setpatientStartCelltoPatientSNPCol & patientSNPRow


--This Dialog assigns the last row of data for file1

display dialog "What row is the last row of you patient SNP data start of the " & " " & patientName & " " & "excel file?" default answer "" buttons {"submit"} default button 1

setpatientLastRowto (text returnedoftheresult)

setpatientLastCelltoPatientSNPCol & patientLastRow


--This Dialog assigns the location of the data copied from file2

display dialog "What column would you like the results of this program stored in of the" & " " & patientName & " " & "excel file." default answer "" buttons {"submit"} default button 1

setresultsLocationto (text returnedoftheresult)


--opens File2. Names it relativeName

set relativeFile to choose file with prompt "Please select the relative Excel file:"

setrelativeNametonameof (info forrelativeFile)

tell application "Microsoft Excel"

open relativeFile

settherelativeFiletoworkbookrelativeName

setrelativeSheettosheet (nameofactive sheet) oftherelativeFile

end tell


--This dialog assigns the column of data in file2

display dialog "What Column is your relative SNP data of the " & " " & relativeName & " " & "excel file?" default answer "" buttons {"submit"} default button 1

setrelativeSNPColto (text returnedoftheresult)


--This dialog assigns the column of data begining copied to excel file1.

display dialog "What Column contains the data you would like copied to the " & " " & patientName & " " & "excel file in column" & " " & resultsLocation & " " & "?" default answer "" buttons {"submit"} default button 1

setcopyAnswerto (text returnedoftheresult)


tell application "Microsoft Excel" -- get values

seto's patientSNP_listtovalueofrange (PatientSNPCol & patientSNPRow & ":" & PatientSNPCol & patientLastRow) ofpatientSheet

setlastrowtofirst row indexof (lastcellofused rangeofrelativeSheet)

seto's relativeSNP_listtovalueofrange (relativeSNPCol & "1:" & relativeSNPCol & lastrow) ofrelativeSheet

seto's copyAnswerListtovalueofrange (copyAnswer & "1:" & copyAnswer & lastrow) ofrelativeSheet

end tell


settcto (counto's relativeSNP_list)

repeatwithifrom 1 totc --convert list of lists to one list

setitemiofo's relativeSNP_listtoitem 1 ofitemiofo's relativeSNP_list

endrepeat


settcto (counto's patientSNP_list)

repeatwithifrom 1 totc -- AppleScript find method, not the Excel Find

o's findLookAtWhole(item 1 of (itemiofo's patientSNP_list))

endrepeat


tell application "Microsoft Excel" -- set values

setvalueofrange (resultsLocation & patientSNPRow & ":" & resultsLocation & patientLastRow) ofpatientSheettoo's resultsList

end tell

endmain

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


If you have lots of rows and you want it to go faster.

I have another script that uses the power of (/usr/bin/awk and his associative arrays) in a do shell script


17000 rows --> awk : 2 seconds, applescript : 6 seconds, total : 8 seconds

34000 rows --> awk : 3 seconds, applescript : 11 seconds, total : 14 seconds

Please Help. compare two excel files. Can't get the find command to work

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