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

Putting Spreadsheet Names in Headers or Footers

One of the things you can add to your page header or footer is the name of your workbook file name. Here's how to make the ...

Discover More

Saving Personalized Copies of a Document

Need a series of documents that include an individual's name or a company name? Here's a handy little macro that will make ...

Discover More

Including Section Numbers in an Index

When you use Word to create your index, you'll normally only include a page number in the index. If you want to create an ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Deleting Conditional Formatting

After you've applied a conditional format to a cell, you may have a need to later delete that format so that the cell is ...

Discover More

Conditional Page Breaks

Need to have your worksheet printout start on a new page every time a value in a column changes? There are a couple of ways ...

Discover More

Sorting Conditional Formats Properly

Conditional formatting can be a great tool to get your data looking just the way you need. However, when you sort data that ...

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

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.