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

10

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, and 2016. 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

A Picture Is Worth a Thousand Words

Nothing beats a screen shot when you are trying to convey information about using the computer. With just a couple of ...

Discover More

Using Copy and Paste for Formatting

Want to copy formatting from one cell and paste it into another cell? It's easy to do if you use the Paste Special ...

Discover More

Disabling a Function Key

Function keys are used to perform common tasks in Excel. If you want to disable one of the function keys, it's rather ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Highlighting Cells Containing Specific Text

If you want to highlight cells that contain certain characters, you can use the conditional formatting features of Excel ...

Discover More

Returning a Value Based on Text Color

Conditional formatting rules can be used to adjust the way in which information is displayed in Excel, such as the text ...

Discover More

Conditional Formatting for Errant Phone Numbers

Conditional formatting can be used to draw attention to all sorts of data based upon the criteria you specify. Here's how ...

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 three minus 3?

2018-07-27 07:01:00

Prabhatt

Your provided information is really valuable. I have a query, what if, I want excel to detect the sound problem of somebody like Sh, Zsh in english phonetics and result not matched or matched depending upon the result. I am a english communication trainer so want to develop this kind of program where I will give earphone to candidate a ask him to pronounce certain words and if his pronunciation is wrong excel should give value 0.

Looking forward for your help!

Thank you!


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.