How to make inactive when I close application window by Command + W

You can do this with Keyboard Maestro.
Create a macro group just for Excel, then create a macro that executes upon ⌘W which closes the window and switches focus to Finder.

                 


I'm not sure you can really have both…

Telling Excel to close a window doesn't tell Excel to quit. It doesn't actually tell it anything other than you closed a window. It sits there waiting for you to tell it what to do next; it can't guess what your next action is going to be.

Of course, if you Quit it, then it goes away & the last-used app will be frontmost.

Clicking the desktop is the quickest way to bring the Finder to the front.


Update 1

Based on the comments made by ironsand to grgarside's answer. Step 6 should be changed to:

tell application "System Events" to set activeApp to name of application processes whose frontmost is true
if length of activeApp is not 0 then
    tell application (first item of activeApp) to activate
end if

My Answer to Posted Question

This can be done by creating a Service using the Automator application. However, you will not be able to use the the COMMAND+W key. I would suggest using the CONTROL+W key since Excel has not yet assigned this key to any function.

  1. Open the Automator Application and click on New Document.
  2. Select Service and click Choose.
  3. Select File->Save from Menus and save service as "Close and Activate Finder". You should see the following window. (Hint: "Open Image in a New Window" for a better view.)

enter image description here

  1. Drag "Close Excel Workbooks" to "Drag actions or files here to build your workflow" window. In the pop up window, click on "Don't Add".
  2. Drag "Run AppleScript" to "Drag actions or files here to build your workflow" window.
  3. Add the text line activate application "Finder" after the text line (* Your script goes here *).

The window should now appear as shown below.

enter image description here

  1. Under "Service receives selected" choose "no input".
  2. Change "any application" to "Other...". In the pop-up window, choose "Microsoft Excel". The window should now appear as shown below.

enter image description here

  1. Select File->Save from Menus.
  2. Select Automator->Services->Services Preferences... from the Menus. Find "Close and Activate Finder" at the bottom of the available services.
  3. Click twice on the word "none" to the right of "Close and Active Finder". Enter the CONTROL-W key. The window should now appear as shown below.

enter image description here

  1. Quit System Preferences and Automator applications.

  2. Test using the Finder and Excel applications.