ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   How can I play a .WAV file in Excel if a condition is met? (https://www.excelbanter.com/excel-worksheet-functions/85659-how-can-i-play-wav-file-excel-if-condition-met.html)

Roy

How can I play a .WAV file in Excel if a condition is met?
 
Can anyone please tell me how I can play a .WAV file in Excel if a condition
is met using the IF statement?



Bernie Deitrick

How can I play a .WAV file in Excel if a condition is met?
 
Roy,

Right click the sheet tab, select "View Code", and paste this code into the window that appears.
This assumes that you have a formula like

=IF(C1="Test", "True", "False")

in cell A1 of that sheet.

Private Sub Worksheet_Calculate()
If Range("A1").Value = "True" Then
PlayMySound ("C:\Windows\Media\Jungle Error.wav")
End If
End Sub

In a regular codemodule in the same workbook, paste this code:

Declare Function sndPlaySound32 Lib "winmm.dll" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Sub PlayMySound(myFileName As String)
Call sndPlaySound32(myFileName, 0)
End Sub

HTH,
Bernie
MS Excel MVP


"Roy" wrote in message
...
Can anyone please tell me how I can play a .WAV file in Excel if a condition
is met using the IF statement?





Roy

How can I play a .WAV file in Excel if a condition is met?
 
Bernie,
Thanks for that, how do I copy the second piece of code into a regular
codemodule?

Roy

"Bernie Deitrick" wrote:

Roy,

Right click the sheet tab, select "View Code", and paste this code into the window that appears.
This assumes that you have a formula like

=IF(C1="Test", "True", "False")

in cell A1 of that sheet.

Private Sub Worksheet_Calculate()
If Range("A1").Value = "True" Then
PlayMySound ("C:\Windows\Media\Jungle Error.wav")
End If
End Sub

In a regular codemodule in the same workbook, paste this code:

Declare Function sndPlaySound32 Lib "winmm.dll" Alias _
"sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Sub PlayMySound(myFileName As String)
Call sndPlaySound32(myFileName, 0)
End Sub

HTH,
Bernie
MS Excel MVP


"Roy" wrote in message
...
Can anyone please tell me how I can play a .WAV file in Excel if a condition
is met using the IF statement?






Bernie Deitrick

How can I play a .WAV file in Excel if a condition is met?
 
Roy,

Copy the code, press Alt-F11 from Excel to open the VBEditor, press F4 to open the project explorer,
select your workbook, then use Insert / Module command to insert a regular codemodule. Then paste
the code into the window to the (usually) right.

HTH,
Bernie
MS Excel MVP

Bernie,
Thanks for that, how do I copy the second piece of code into a regular
codemodule?

Roy





All times are GMT +1. The time now is 01:53 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com