Skip to main content
 

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

Author: Xiaoyang Last Modified: 2024-08-08

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


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:

a screenshot showing how to play default system beep sound when a cell value matches a certain condition

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


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:

a screenshot showing how to play a custom sound when a cell value matches a certain condition

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:

a screenshot of selecting a sound


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

a screenshot of using a VBA code

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.


Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!