• RyanDev.com

  • How to play a sound in Microsoft Excel when a value changes

7th November 2008

How to play a sound in Microsoft Excel when a value changes

Many people use Excel to analyze data and to “watch” data.  Did you know that you can have Excel play a sound or even a song when a cell changes if you want to alert someone?  In fact, you could trigger any kind of workflow you wanted.  This example shows how to call into the Win32 API using Excel to play a sound.

We first need to declare to Excel how to call into the Win32 API.  We also want to declare a public variable to keep track of the value in cell A1 so we can know when it changes.

First, add a module to the workbook that you will be using and add the following 2 lines of code:

Declare Function sndPlaySound32 Lib “winmm.dll” Alias “sndPlaySoundA” (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
 
Public CellValue As Variant ’store the cell value here

The first line is how you make Excel aware of the Win32 API call to play a sound.  The second line of code is declaring the global variable.

Second, we want to read in the value of cell A1 when the workbook first opens so we can keep track of when it changes.  Use the following code:
Private Sub Workbook_Open()
CellValue = Sheets(1).Range(”a1″)
End Sub

Now that we are storing what the value is and we have told Excel how to play the sound we now need to check the cell each time something on that sheet changes.  Use the following code:

Private Sub Worksheet_Calculate()
If Range(”a1″) > 1 And CellValue <> Range(”a1″) Then
    sndPlaySound32 “C:\Error.WAV”, 0
    CellValue = Range(”a1″)
End If
End Sub

‘if you want it to play the sound every time anything on the sheet changes, delete out the
‘parts about CellValue

Obviously you can add in any logic you would like.  You could choose to send an email, print the document to the printer or even using Unified Communications (UC) services (from companies such as http://www.engage2day.com) you could have the system IM someone or even have the system call them and let them know a threshold has been reached.  This may be overkill for an Excel spreadsheet but it certainly opens up the possibilities of what you can do with a simple Excel sheet.

This entry was posted on Friday, November 7th, 2008 at 8:52 pm and is filed under Microsoft Excel, Microsoft Office. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

There are currently 7 responses to “How to play a sound in Microsoft Excel when a value changes”

Why not let us know what you think by adding your own comment! Your opinion is as valid as anyone elses, so come on... let us know what you think.

  1. 1 On January 27th, 2009, Michael said:

    Hi. Do you happen to know if you could do something like this but for a step in a Dynamics CRM workflow? I read that ISVs have to create plugins for CRM in order to add workflow step libraries, but I don’t really know my way around .NET languages. Left my email if you want to respond. Thanks!

  2. 2 On March 9th, 2009, viadiatub said:

    now in my rss reader)))
    ————————

  3. 3 On March 10th, 2009, RadsNerrels said:

    yo, great name for site)))
    ————————

  4. 4 On March 11th, 2009, admin said:

    Yes you certainly can register a custom workflow step. The best resource on how to do this is the CRM SDK which can be downloaded at http://www.microsoft.com/downloads/details.aspx?FamilyID=82E632A7-FAF9-41E0-8EC1-A2662AAE9DFB&displaylang=en. Or, if you provide specific details, I may be able to provide additional help.

  5. 5 On October 14th, 2009, jeff said:

    Very cool!

  6. 6 On June 29th, 2010, Remo said:

    I saw this really good post today…

  7. 7 On May 12th, 2011, sydney said:

    This was a actually extremely good submit. I

Leave a Reply

  • Links

  • Calendar

  • February 2012
    S M T W T F S
    « Jan    
     1234
    567891011
    12131415161718
    19202122232425
    26272829