Excel

Make a Sound When a Cell is Changed to a Certain Value

Ease of Use

Easy

Version tested with

2002 

Submitted by:

Jacob Hilderbrand

Description:

This macro plays a wav file when any cell in a specified range is above a certain value. 

Discussion:

You have several cells in a worksheet that calculate data. If these cells change to be greater than (or less than) a preset value, you want an immediate notification. This macro will play any wav file that you specify. 

Code:

instructions for use

			

Option Explicit Private Declare Function sndPlaySound32 Lib "winmm.dll" _ Alias "sndPlaySoundA" (ByVal lpszSoundName _ As String, ByVal uFlags As Long) As Long Private Sub Worksheet_Change(ByVal Target As Range) Dim Cell As Range Dim CheckRange As Range Dim PlaySound As Boolean Set CheckRange = Range("D5:G25") For Each Cell In CheckRange If Cell.Value > 10 Then PlaySound = True End If Next If PlaySound Then Call sndPlaySound32("C:\windows\media\ding.wav", 1) End If End Sub

How to use:

  1. Copy the code above.
  2. Open Excel.
  3. Right-click the sheet tab on which you want the sound to play, and hit View-Code.
  4. Paste the code in the Code Window that opens to the right.
  5. Change the range for the CheckRange variable as needed.
  6. Change this line: { If Cell.Value > 10 Then } to indicate the value you want the code to work with.
  7. Change the path and the name of the wav file as needed.
  8. Close the Visual Basic Editor (Alt + Q or press the x in the top right corner).
 

Test the code:

  1. Change values in the worksheet. If a cell within CheckRange is changed to the appropriate value, the wav file plays.
 

Sample File:

Play Wav.ZIP 6.88KB 

Approved by mdmackillop


This entry has been viewed 230 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express