Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 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

by Allen Wyatt
(last updated November 17, 2018)

3

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 Office 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

Searching for Special Hyphens

Word allows you to use a couple of different types of hyphens in your document, each with a different purpose. If you ...

Discover More

Accessing Dependent and Precedent Information

The auditing tools provided in Excel can provide some very helpful information about how your formulas and data are ...

Discover More

Changing Colors of Spelling and Grammar Underlines

The red and green wavy underlines used in Word can be a boon for proofing a document, but they are of little use if you ...

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 Formats that Distinguish Blanks and Zeroes

Conditional formatting is a great tool. You may need to use this tool to tell the difference between cells that are empty ...

Discover More

Applying Conditional Formatting to Multiple Worksheets

If you just updated your copy of Excel, you may have noticed some differences in how the program handles applying ...

Discover More

Working with Multiple Conditions

When you apply conditional formatting, you are not limited to using a single condition. Indeed, you can set up multiple ...

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. 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 4 - 0?

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.