Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Conditionally Making a Sound.
Written by Allen Wyatt (last updated July 15, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Ken knows how to create conditional formats in Excel. What he really wants to do, however, is have Excel make an audible sound (a beep or whatever) if the conditions are met.
There is no way to do this without resorting to using macros. If you just want to make a beep sound, you can use something like this:
Function BeepMe() As String Beep BeepMe = "" End Function
All this user-defined function does is to play a sound (which will vary depending on the system you are using) and then return an empty string. You can use the function in your worksheet in this manner:
=IF(A12>300,BeepMe(),"")
If you want to play some sound other than the default system beep, you'll need to use the Windows API PlaySound function. The following code creates a user-defined function that will play the default "tada" sound so prevalent in many versions of Windows.
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 Function SoundMe() As String Call PlaySound("c:\windows\media\tada.wav", _ 0, SND_ASYNC Or SND_FILENAME) SoundMe = "" End Function
This function can be called the same as the previous example:
=IF(A12>300,SoundMe(),"")
If you want to play a different WAV file, simply change the file specification in the SoundMe function.
Note that the code needs to be entered in an inserted module in the VBA Editor in order for the function to work properly.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5834) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Conditionally Making a Sound.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Setting up conditional formatting can be challenging under some circumstances, but once set it can work great. Unless, of ...
Discover MoreConditional formatting is a powerful tool you can use to dynamically adjust the formatting of your worksheet. This tip ...
Discover MoreConditional formatting does not allow you to change the typeface and font size used in a cell. You can write your own ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-01-20 15:18:28
J. Woolley
@Jeff
My Excel Toolbox includes the following function to play a sound, including a .wav file:
=BeepThis([ThisSound], [ThisValue], [ThisCount], [Wait])
The four optional arguments are described in my previous comment below. For your requirement (assuming cell A1), try something like this:
=IF(A1>0,BeepThis("Tada",A1),IF(A1<0,BeepThis("Chord",A1),A1))
ThisSound can be set to the full path of any .wav file on your system; for example: "C:\Windows\Media\Windows Logon.wav"
%SystemRoot%\Media (usually C:\Windows\Media) is the default folder and .wav is the default type, so this would match the example: "Windows Logon"
My Excel Toolbox also includes the following text-to-speech function:
=Speak(This)
where This is a range or value (text, numeric, logical, or error). So you could simply speak the value of A1 like this:
=Speak(A1)
See https://excelribbon.tips.net/T012857
And https://sites.google.com/view/MyExcelToolbox/
2024-01-19 14:44:04
Jeff
Is there a way to have multiple .wav sounds go off based on condition? For example, I'd like to have audio when the cell is >0 and a different audio when <0.
Thank you,
Jeff
2018-11-28 15:10:27
J. Woolley
Here is my version of BeepThis. (Apologies for poor VBA code format when posting comments here.)
#If VBA7 And Win64 Then ' 64-bit Excel under 64-bit Windows
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As LongPtr) As LongLong
#ElseIf VBA7 Then ' 64-bit Excel in all environments
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As LongPtr, ByVal dwFlags As LongPtr) As Long
#Else ' 32-bit Excel
Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If
Const SND_SYNC = &H0 ' wait for sound to play
Const SND_ASYNC = &H1 ' no wait
Const SND_ALIAS = &H10000 ' play system event sound
Const SND_FILENAME = &H20000 ' play sound filepath
Public Function BeepThis(Optional ByVal ThisSound As String = "Beep" _
, Optional ByVal ThisValue As Variant _
, Optional ByVal ThisCount As Integer = 1 _
, Optional ByVal Wait As Boolean = False) As Variant
Dim sPath As String, flags As Long
If IsMissing(ThisValue) Then ThisValue = ThisSound
BeepThis = ThisValue
If ThisCount > 1 Then Wait = True
flags = -(Not Wait)
sPath = LCase(ThisSound)
Select Case sPath
Case "beep"
Beep ' ignore ThisCount and Wait
Exit Function
Case "asterisk", "default", "exclamation", "hand", "notification", "question"
sPath = "System" + StrConv(sPath, vbProperCase)
flags = (flags Or SND_ALIAS)
Case "chimes", "chord", "notify", "ringout", "tada"
sPath = Environ("windir") + "\media\" + sPath + ".wav"
flags = (flags Or SND_FILENAME)
Case Else
sPath = ThisSound
flags = (flags Or SND_FILENAME)
End Select
Do While ThisCount > 0
PlaySound sPath, 0, flags ' if error, default sound will play
ThisCount = ThisCount - 1
Loop
End Function
Syntax: BeepThis([ThisSound], [ThisValue], [ThisCount], [Wait])
All four arguments are optional.
ThisSound is a text value (case is ignored) indicating the sound to play (default is "Beep"). The following values are recognized:
"Beep" - standard VBA Beep (one time only)
"Asterisk", "Default", "Exclamation", "Hand" (aka Critical Stop), "Notification", "Question" - standard Windows system sounds (see Control Panel > Sound or right-click Taskbar > Speaker and pick Sounds)
"Chimes", "Chord", "Notify", "Ringout", "Tada" - WAV files usually found under C:\Windows\media
ThisSound can also be set to the path of any WAV file on your system; for example, "C:\Windows\media\Windows Logon.wav"
If ThisSound cannot be played (for example, ThisSound=""), the system "Default" sound will play (if available).
ThisValue is the value (text, numeric, etc.) returned by BeepThis. If ThisValue is missing, the text value of ThisSound will be returned.
Unless ThisSound is "Beep" it will play ThisCount times (default is one time). Be careful with ThisCount (perhaps 3 or less) because Excel will be unresponsive while ThisSound is repeated; press the Esc key to interrupt play. If ThisCount<1, no sound will play unless ThisSound is "Beep" (the default).
If Wait is TRUE, BeepThis will wait for the ThisSound to finish play before returning. If Wait is FALSE (the default value), BeepThis will not wait. If ThisCount>1, then Wait will be forced TRUE. If ThisSound="Beep" (the default), then Wait will be ignored.
For example, if the formula in a cell is:
=BeepThis() - play the standard VBA Beep and return "Beep"
=BeepThis(,"Once",10) - play the VBA Beep once (ignore ThisCount) and return "Once"
=BeepThis("Tada",A1,A1) - play "Tada" A1 times (unless A1<1) and return A1
=IF(WEEKDAY(TODAY(),2)>5,BeepThis("Hand","Weekend",2),"Workday")
Notice no sound is played unless the cell's formula is recalculated and ThisCount>0 or ThisSound="Beep" (the default).
2018-11-17 11:07:59
Vesa Nuutinen
Hello, is there easy way to make this work with 64 bit office?
2018-11-17 10:20:56
Hans Hallebeek
If the sound file is not presnt en 64 32 bit
Option Explicit
#If VBA7 Then
Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
#Else
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long
#End If
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Function SoundMe() As String
On Error Resume Next
Call PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_FILENAME)
If Err.Number <> 0 Then BeepMe
Err.Clear
On Error GoTo 0
SoundMe = ""
End Function
Function BeepMe() As String
Beep
BeepMe = ""
End Function
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments