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

Adding a Missing Closing Bracket

When working with large amounts of data, it is a good idea to make sure that the data all consistently follows a pattern. ...

Discover More

Keep Your Headings in View

Headings on a table are very important when it comes to understanding what is in the table. This tip explains an easy way you ...

Discover More

Detecting an Open Dialog Box

Macros can be used to perform all sorts of tasks within Word. Some tasks can even occur at whatever time interval you desire. ...

Discover More

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!

More ExcelTips (ribbon)

Conditionally Formatting Non-Integers

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

Discover More

Changing Font Face and Size Conditionally

Conditional formatting does not allow you to change the typeface and font size used in a cell. You can write your own macro ...

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. 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 three minus 2?

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.