Cookies help us deliver our services. By using our services, you agree to our use of cookies.
Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to play a sound if a condition is met in Excel?

In Excel, we can apply the Conditional Formatting to format and highlight the cells to meet the condition as you need, but, sometimes, you may want to play a sound if a condition is met. For example, if cell value in A1 is greater than 300, I want a sound is played. Excel does not support this feature, this article, I will introduce some VBA codes to solve this task.

Play a default system beep sound based on cell value with VBA code

Play a custom sound based on cell value with VBA code

Play a sound if cell value changes in a specific column with VBA code


arrow blue right bubble Play a default system beep sound based on cell value with VBA code

Here is a handy code for you to play a default system beep sound when a specific condition is met, please do as this:

1. Hold down the ALT + F11 keys, then it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Play a default system beep sound based on a cell value:

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

3. Then save and close this code window, go back to the worksheet, and enter this formula: =IF(A1>300,BeepMe(),"") into a blank cell beside the cell contains the value you want to play a sound based on, and then press Enter key, nothing will be displayed into the formula cell, see screenshot:

doc play sound if conditon true 1

4. And now, if the entered value in cell A1 is greater than 300, a default system beep sound will be played.


arrow blue right bubble Play a custom sound based on cell value with VBA code

If you want to play some other sound than the default system beep sound, here also a VBA code can do you a favor.

1. Hold down the ALT + F11 keys, then it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Play a specific sound based on a cell value:

#If Win64 Then
    Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
#Else
    Private Declare Function PlaySound Lib "winmm.dll" _
        Alias "PlaySoundA" (ByVal lpszName As String, _
        ByVal hModule As Long, ByVal dwFlags As Long) As Boolean
#End If
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Function SoundMe() As String
'Updateby Extendoffice 20161223
    Call PlaySound("c:\windows\media\Speech On.wav", _
      0, SND_ASYNC Or SND_FILENAME)
    SoundMe = ""
End Function

3. Then save and close this code window, return to the worksheet, and enter this formula: =IF(A1>300,SoundMe(),"")into a blank cell beside the cell contains the value you want to play a sound based on, and then press Enter key, nothing will be displayed into the formula cell, see screenshot:

doc play sound if conditon true 2

4. From now on, if a value greater than 300 is entered into cell A1, a specific sound will be played at once.

Notes: In the above code, you can change the sound wav file to your need from c:\windows\media\ file path. See screenshot:

doc play sound if conditon true 3


arrow blue right bubble Play a sound if cell value changes in a specific column with VBA code

If you want to play a sound if cell value changes in a specific column, you can apply the following VBA code.

1. Right click the sheet tab that you want to play a sound when value changes in a column, then choose View Code from the context menu, and in the opened Microsoft Visual Basic for applications window, copy and paste the following code into the blank Module:

VBA code: Play a sound if cell value changes in a column:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20161223
Dim xCell As Range
On Error Resume Next
If Target.Columns.Count = 1 Then
  If Intersect(Target, Columns(3)) Is Nothing Then
    Exit Sub
  Else
    For Each xCell In Columns(3)
        On Error Resume Next
        If (xCell.Value = Target.Value) And (xCell.Value <> "") Then
          Beep
          Exit For
        End If
     Next
  End If
End If
End Sub

doc play sound if conditon true 4

Note: In the above code, the number 3 in the script Columns(3) is the column number which you want to play sound when value changes in this column.

2. And then save and close this code window, now, if a cell value changes in the third column, a default system beep sound will be played.



Recommended Productivity Tools

Office Tab

gold star1 Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

Kutools for Excel

gold star1 Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

gold star1 300 New Features for Excel, Make Excel Much Easy and Powerful:

  • Merge Cell/Rows/Columns without Losing Data.
  • Combine and Consolidate Multiple Sheets and Workbooks.
  • Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
  • Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
  • More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Screen shot of Kutools for Excel

Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Excelakos · 1 years ago
    Hi there
    I try to use the Play a custom sound based on cell value with VBA code but i have a problem. Here is an example:
    I have a time countdown in cell A1 which means value changes per second.
    Then in cell B1 i have inserted =8/(24*60) in a 0:08:00 format
    So the if i use to trigger the sound is if B1>A1,SoundMe(),""
    But the sound will never be heard. I can hear a sound like something is looping and i guess it has something to do with the fact that the value is dynamically changing.
    Then i tried to alter the code in the part Call PlaySound("c:\windows\media\Speech On.wav", _
    0, SND_ASYNC Or SND_FILENAME)
    I changed SND_ASYNC with SND_SYNC.

    This way i managed to play the sound, but it gets repeating forever.

    Is there any solution like to trigger the sound only once meaning just the very first time the criteria is met and then stop ??
    Thank you a lot