Office:mac Auto Color Cells

I recently switched to a Mac and really miss my auto color cells VBA script for Excel. After quite a bit of digging (and trial-and-error) I managed to recreate the functionality using AppleScript. It is pretty slow, but it works!

Just like the VBA version, this automatically color codes cells to help identify inputs, formulas, etc. For example, cells that contain only numbers are colored blue, all formulas black, references to other workbooks are green and cells that include the =OFFSET() function (what I use for setting up different scenarios) are rust.

Open up the AppleScript Editor, paste the following code and save it as /Users/<your-username>/Documents/Microsoft User Data/Excel Script Menu Items/AutoColorCells\scc.scpt. The \scc in the filename creates a keyboard shortcut control-shift-c.

tell application "Microsoft Excel"
    --activate
    set colStart to (get first column index of selection)
    set rowStart to (get first row index of selection)
    set colCount to (get count columns of selection)
    set rowCount to (get count rows of selection)

    set status bar to "Auto coloring the cells"
    set screen updating to false

    repeat with i from rowStart to rowStart + rowCount - 1
        set status bar to "Row " & i - rowStart + 1 & " of " & rowCount
        repeat with j from colStart to colStart + colCount - 1
            --set theCell to cell j of row i
            --set f2 to (get formula of theCell)
            set f to (get formula of cell j of row i)

            if f starts with "=" then
                if f contains ".xls" then
                    set font color index of font object of cell j of row i to 10 -- rust
                else if f contains "OFFSET" then
                    set font color index of font object of cell j of row i to 9 -- green
                else if my isallnumbers(f) then
                    set font color index of font object of cell j of row i to 5 -- blue
                else
                    set font color index of font object of cell j of row i to 1 -- auto
                end if
            else
                if my isallnumbers(f) then
                    set font color index of font object of cell j of row i to 5 -- blue
                else
                    set font color index of font object of cell j of row i to 0 -- auto
                end if
            end if
        end repeat
    end repeat

    set screen updating to true
    set status bar to "Auto Color Done"

end tell

to isallnumbers(f)
    set l to length of f

    --log_event("f is " & f) of my commonScript

    repeat with k from 1 to l
        set c to ASCII number of character k of f
        if c < 40 or c > 61 then
            return false
        end if
    end repeat
    return true
end isallnumbers

Published

February 28, 2010 7:00PM

License

The contents of this blog are licensed under the Creative Commons “Attribution-Noncommercial-Share Alike 3.0″ license.