If you want to conditionally make a sound in Excel, there are a few things you'll need to do. First, open up the Visual Basic Editor (VBE) by pressing Alt+F11. Next, insert a new module by going to Insert > Module. In the module, paste the following code:
Sub PlayWAVFile() Dim WaveFile As String WaveFile = "C:\Windows\Media\tada.wav" Call PlaySound(WaveFile, SND_ASYNC Or SND_FILENAME) End Sub
This code will play the tada.wav file located in your Windows Media folder when called. You can change the file path to any other .wav file on your computer.
Now that you have the code written, you need to create a macro that will call the code. To do this, go to View > Macros > View Macros. In the Macro dialog box that appears, enter a name for your macro and click the "Create" button. Paste the following code into the macro:
Sub PlaySoundWhenCellIsChanged() If Target.Address = "$A$1" Then Call PlayWAVFile End If End Sub
This macro will call the PlayWAVFile subroutine whenever cell A1 is changed. You can change the cell reference to any other cell in your worksheet.
The last thing you need to do is assign the macro to the Worksheet_Change event. To do this, double-click on your worksheet tab ( Sheet1 , for example) and paste the following code into the code window that appears:
Private Sub Worksheet_Change(ByVal Target As Range) Call PlaySoundWhenCellIsChanged End Sub