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

9

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5834) applies to Microsoft Excel 2007, 2010, and 2013. 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

Cropping Graphics

Need your hide some of the outside edges of a graphic? You can instruct Word to crop (or hide) those outside edges by ...

Discover More

Identifying the Last Cell Changed in a Worksheet

When someone changes a cell in a worksheet, Excel normally goes along its merry way of keeping everything up to date. It does ...

Discover More

Placing Formula Results in a Comment

Excel won't allow you to directly or automatically insert the results of a formula into a cell's comment. You can, however, ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Conditionally Highlighting Cells Containing Formulas

Excel's conditional formatting feature allows you to create formats that are based on a wide variety of criteria. If you want ...

Discover More

Conditionally Formatting for Multiple Date Comparisons

When you compare dates in a conditional formatting rule, you need to be careful how you put your comparisons together. Do it ...

Discover More

Coloring Identical Company Names

Want to know where duplicates are in a list of names? There are a couple of ways you can go about identifying the duplicates, ...

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 four less than 9?

2016-02-04 04:50:43

Barry

@Kristian

See my comments on 08 August 2014 05:09


2016-02-03 07:50:55

Kristian Johannsen

This Exceltip really lacks information on where to put the code, in order to get this to work.


2015-11-13 19:21:52

Ed Vagim

I have set up the following code.

Function BeepMe() As String
Beep
BeepMe = ""
End Function

I have set up the follow code to run the above macro when cell A621 turns "FALSE"

=IF(A621=FALSE(),BeepMe(),"")

What am I doing wrong. It does not work.


2015-08-22 08:47:09

Klaus-D. Hermeneit

the actual sound of "Beep" (Version Windows7/Excel 2007 is poor compared with the sound WindowsXP/Excel 2000.
Is there a possibility to return to the old Sound of Beep???


2014-08-11 06:03:24

Tangmere SM

FAO Barry,

Many thanks for this, the formula does now work well.

Now, can you think of anyway I could get this formula only do a lookup of the cells every 2 minutes?

The problem I have is that I have 3 queries attached to this spreadsheet, and each time a query runs (auto refresh every minute), the formula plays a sound 3 times (3 queries).

Can you think of any way of restricting this?

Many thanks in advance.

Tangmere SM.


2014-08-08 05:09:06

Barry

@ Tangmere SM

This code will not work if it enetered on the codepage of a worksheet (even the one where the formula resides). - I just tried this in Excel 2010.

However, if you insert a Module (in the VB Editor click Insert then Module)it works perfectly.


2014-08-07 06:28:39

Tangmere SM

This is the exact code that I am using in Excel 2013;

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:sound1.wav", _
0, SND_ASYNC Or SND_FILENAME)
SoundMe = ""
End Function

Using formula;

=IF(A1<7,SoundMe(),"")

Can you see anything obvious wrong with this?

Or could it be the version of Excel?


2014-08-06 07:43:45

awyatt

The #NAME? error is returning because it cannot understand the SoundMe() portion. That tells me you haven't correctly created the SoundMe macro in your system. It needs to look like what is shown in the tip.


2014-08-06 05:14:13

Tangmere SM

Hi,

I have applied this VBA code and then the formula =IF(A12>300,SoundMe(),"") into my workbook, but all it returns is #NAME?.

Can you tell me what I could be doing wrong?

:)


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.