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

Converting to Hyperlinks in a Shared Workbook

When you enter a URL or e-mail address in a worksheet, Excel usually converts it to a clickable hyperlink. This doesn't ...

Discover More

Preventing Printing

Want to prevent your worksheet from being printed? You may be out of luck, as a determined person may be able to find a ...

Discover More

Uppercase and Lowercase AutoCorrect Entries

AutoCorrect can be a great tool to correct, automatically, the typos and wording you enter in a document. Sometimes, ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Highlighting Cells Containing both Letters and Numbers

Conditional formatting is a great tool for changing the format of cells based on whether certain conditions (rules) are ...

Discover More

Conditionally Formatting Non-Integers

The conditional formatting capabilities of Excel are very helpful when you want to call attention to different values ...

Discover More

Conditionally Formatting Cells Containing Dates

Need to know if a particular cell contains a date value? Excel doesn't have a worksheet function to determine this ...

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 five minus 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.