Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Adding a Little Animation to Your Life
Converting a Range of URLs to Hyperlinks
Making the Formula Bar Persistent
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 versions: 97 2000 2002 2003 2007
Don't Go in Debt for Christmas! Tired of trying to keep up with the Joneses for Christmas? Want to enjoy the season rather than dread the aftermath? Learn how you can avoid the financial traps that spring up every Christmas.