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.

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


5

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:

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 (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.

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

Using a Protected Worksheet

If you have a worksheet protected, it may not be immediately evident that it really is protected. This tip explains some ...

Discover More

Calculating Monthly Interest Charges

Trying to calculate how much people owe you? If you charge interest or service charges on past-due accounts, there are a ...

Discover More

Using AutoCorrect

The AutoCorrect feature in Excel is a great tool for quickly entering information. Here's an explanation of the feature ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Conditional Format that Checks for Data Type

Conditional formatting can be used to highlight cells that contain the improper type of data for your needs. This tip ...

Discover More

Diagonal Borders in a Conditional Format

Conditional formatting is a great tool for changing how your data looks based on the data itself. Excel won't allow you ...

Discover More

Noting Inactivity within a Timeframe

There are many times when you are creating a worksheet that you need to analyze dates within that worksheet. Once such ...

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 6 + 5?

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


This Site

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.

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.