Skip to main content

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.

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...

Description


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!
Comments (28)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I have a school project. excel user form using for search a record using barcode.. Problem is that.. i want when trig a barcode specific text box value after update with a sound file each recorded ... means a student name appear in the background. for call on closing time.. pls guide...urgent
This comment was minimized by the moderator on the site
Your article is so good I like it very much, the latest audiobooks 2022 at horbuchkostenlos.de
This comment was minimized by the moderator on the site
Good site I love this website
This comment was minimized by the moderator on the site
Thanks for the article you shared, it was very helpful for me!!!Thank you very much
You can learn the sound here: <a href="https://sonneriesvip.com/">https://sonneriesvip.com/</a>;
This comment was minimized by the moderator on the site
Thanks for the article you shared, it was very helpful. Check out some more <a href="https://yofonts.com/">font free online</a> completely free.
This comment was minimized by the moderator on the site
Bonjour,Le code ne fonctionne pas,
Le code suivant est en rouge:Private Declare PtrSafe Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As LongPtr, ByVal dwFlags As Long) As Boolean
Pour le code ci-dessous, j'ai un fichier mp3.Call PlaySound("c:\windows\media\Speech On.wav", _
0, SND_ASYNC Or SND_FILENAME)
Faut il laisser \Speech On.WavJ'ai essayé avec mp3 mais ça ne fonctionne pas.
Pouvez vous m'aider ?MerciCordialementRobert


This comment was minimized by the moderator on the site
Can i insert a mp3 sound with durations 1 hour?

Thank you very much
You can learn the chakushinon123
This comment was minimized by the moderator on the site
Nice Work! Thank you so much for the code, it was all that I needed
This comment was minimized by the moderator on the site
Not just photos or files. Sometimes you can insert a music file into the EX at https://klingeltonkostenlos.de/klingeltone-filmmusik-gratis/. This is an interesting thing, isn't it?
This comment was minimized by the moderator on the site
Grazie molte. ho utilizzato in modo proficuo tutti i tuoi esempi che sono stati chiari e illuminanti. aiuto prezioso
This comment was minimized by the moderator on the site
I am a business person, using Excel is too familiar. I usually save the names of tracks in execl so that it is simpler to find them than to save them in files. Great.The music I use for business comes from ZigTone.com.You can go there and study them, maybe it's good for you.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations