If you have Microsoft Excel, the formula is in the help under the "correl" function.
I have been doing some work in this area myself. I am coming up with factors for each stock of how closely they correlate to overall market movement. This way, I know that if the market starts moving down and I am long in a stock that correlates pretty closely, I should consider exiting.