Monday, May 2, 2011

AppleScript for Excel development #1

I'm currently working on an AppleScript for another department to handle a piece of heavy lifting in Excel. As I might have stated previously, Excel can be a bit obtuse on how to talk to it until you get to know each other. It really helps, wherever possible, to name each column you expect to use as a range and call them by that name in the script using something like
 tell row x of range myRangeName
 end tell
The current project is intended to perform some formatting, column additions and deletions, as well as comparing the values of two different columns and deleting any rows that have duplicate entries. That last part is one of the most time consuming operations so far. The opening approach was to duplicate what was currently being done manually -- concatenate the two compared cells into one value
 =concatenate(A2&"-"&E2)
 result "20-3154"
The entire sheet is then sorted by the column holding that formula and then a loop compares each cell to the cell above it, and if the values match, deletes the duplicate row. This is unfortunately a time consuming process as each cell in the column must be assigned the formula, and then each cell evaluated. In a sheet that can have thousands of records, this process can take 20 to 30 minutes to crunch.

I re-evaluated the needs and decided I could skip the concatenation and just sort the sheet by both columns, and evaluate them in a nested loop of if/then/else blocks. This cuts out a step, but still is not quite as fast as I'd like; perhaps because a good number of the records are evaluated twice, once for the first column, and then by the second if the first proves true.

I am currently evaluating the autofill command to see if the first method might be faster this way, but I'm having a hard time extracting the proper index references from the ranges I have defined. Partly because Excel speaks to the user in one reference style, and to itself in another. (A1 vs R1C1)

No comments:

Post a Comment