Conditionally Playing an Audio File

by Allen Wyatt
(last updated June 23, 2012)

3

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.

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

Rows in a PivotTable

PivotTables are used to analyze huge amounts of data. The number of rows used in a PivotTable depends on the type of analysis ...

Discover More

Comparing Strings

When writing a macro, a common task is the need to compare two strings. You can do this by "normalizing" the strings, as ...

Discover More

Getting a List of User Accounts

Part of managing a computer system that is shared by multiple people is to keep track of all the user accounts on that ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Changing Your Name

One of the many pieces of information that Excel keeps track of is your name. If you want to change your name for Excel's ...

Discover More

Big File Memory Blues

Excel places limits on how much memory your workbooks can use. Despite these limits, it is possible to create a workbook that ...

Discover More

Understanding Lists

What is a list of data, and how do you create one? Here are some guidelines you may find helpful.

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 three more than 8?

2017-02-06 02:25:20

Karan Salecha

In my excel sheet, I am pulling live data from google and I want excel to alert me when that data exceeds a particular number which is stored in another cell. For that I've written this code but it doesn't seem to work.

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


Sub PlayWAV()
WAVFile = C:\Users\Admin\Desktop\[Excel alert.xlsx]Sheet1 & "C:\Windows\Media\Alarm01.wav"
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("J17").Value > Range("L17").Value Then PlayWAV
End Sub



2015-11-02 11:33:42

Prashant Sinha

Hello friends,

I had seen an excel file that works as piano, pressing different keys gave different notes of same sound. How is it possible to get it done. Please help me. If anyone has such file please mail it to me. Otherwise do let me know how can I get it?

Thanks in advance.
Prashant Sinha


2014-12-19 10:54:32

clyde

If I have the wave file in the D: how would I format the wave file into the macro?


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.