Conditionally Playing an Audio File

Written by Allen Wyatt (last updated April 19, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Tassos would like to have Excel play an audio file when the value in a certain cell exceeds a threshold. For instance, when the value in a cell exceeds 999 he would like a particular sound file to be played.

There is no built-in way to do this in Excel (although it would be an interesting addition to Excel's conditional formatting features). You can, however, play a sound file by using a macro to do a call to the Windows API.

You need to start by placing some code in the Sheet object for the workbook. (Right-click the tab for the worksheet and choose View Code from the Context menu.) Declare the function "playsound" using the following code:

Private Declare Function PlaySound Lib "winmm.dll" _
  Alias "PlaySoundA" (ByVal lpszName As String, _
  ByVal hModule As Long, ByVal dwFlags As Long) As Long

    Const SND_SYNC = &H0
    Const SND_ASYNC = &H1
    Const SND_FILENAME = &H20000

Next you can create a short little macro that will actually play the sound file. Assuming that the sound file is in the same directory as the workbook, the following code will work. (You should modify the code so that it contains the proper filename and location.)

Sub PlayWAV()
    WAVFile = ThisWorkbook.Path & "\MyAudioFile.wav"
    Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub

Finally, establish the criteria when the file is to be played. In this case you want the sound file to play whenever the value in the target cell exceeds the threshold value of 999. The following will check for that condition in cell C5 and, if warranted, play the sound file:

Private Sub Worksheet_Change(ByVal Target As Range)
    Threshold = 999
    If Range("C5").Value > Threshold Then PlayWAV
End Sub

Now, whenever the value in Cell C5 changes and exceeds 999, the audio file will play one time. If the values is changed to less than 999, nothing plays. If the value changes to another value that exceeds 999, the sound file will play again.

For additional ideas on playing audio files, check out these sites:

http://www.j-walk.com/ss/excel/tips/tip87.htm
http://www.cpearson.com/excel/PlaySound.aspx

You should note, as well, that you can get Excel to play a system sound by using data validation. Simply set up the validation criteria (described in other issues of ExcelTips) and then, on the Error tab, specify whether you want Excel to stop, warn, or inform the user. When a value is entered in the cell that does not fit the criteria, a dialog box is displayed and the system sound is heard.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (6559) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Comments in Text Boxes

If you use text boxes in your documents, you may sometime want to place a comment in the text box, the same as you can do ...

Discover More

Automatically Opening Macro Workbooks when Using a Shortcut Key

Click a button on a toolbar and Excel will go so far as to open a another workbook in order to run a macro associated ...

Discover More

Pop-Up Comments for Graphics

Excel allows you to add comments to individual cells in a worksheet, but what if you want to add comments to graphics? ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Selecting Noncontiguous Ranges with the Keyboard

It's easy to select non-contiguous ranges using the mouse, but may seem more daunting if you are simply using the ...

Discover More

Stopping Help from Using Online Resources

The two newest versions of Excel rely upon the Internet to grab help information. If you don't want Excel to seek help ...

Discover More

Backing Up Your Customized Toolbars

After customizing your Excel toolbars, it is a good idea to make a backup of the file that contains the information. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 9 - 8?

2022-08-12 09:09:01

Thomas K. Trower

I had used this routine successfully until 2016.

It looked like this:

(see Figure 1 below)

[en] was part of a value lookup to randomly select between 128 WAV files to play.

When I transcribe the coding to excel 2016 I just get a "bring" sound.
It looks as though the program executes, but the files do not play.

Please advise.

Thomas K. Trower

Figure 1. 


2019-03-28 20:19:46

Erik van der Neut

Do you know how to do this on a Mac as well? I have a spreadsheet with a list of audio file paths, and when I change my row selection, would love the audio from that row being auto-played. Able to do this in Windows, but that script doesn't work on the Mac, because it's looking for the Windows DLL.

Any chance this script can be adapted to work on the Mac as well?

Thanks!
Erik


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.