Looping and adding sound

Help for Heroes

New Member
Joined
Apr 7, 2013
Messages
1
Hi, I'm trying to add a .wav sound to all cells in a column if it equals "! ALERT !". I have created a macro and added the following code:

Private DeclareFunction PlaySound Lib "winmm.dll" _<o:p></o:p>
Alias "PlaySoundA" (ByVal lpszNameAs String, _<o:p></o:p>
ByVal hModule As Long, ByVal dwFlags As Long)As Long<o:p></o:p>
<o:p> </o:p>
Const SND_SYNC = &H0<o:p></o:p>
Const SND_ASYNC = &H1<o:p></o:p>
Const SND_FILENAME = &H20000<o:p></o:p>
<o:p></o:p>
Sub PlayWAV()<o:p></o:p>
WAVFile = ThisWorkbook.Path &"\Windows Exclamation.wav"<o:p></o:p>
Call PlaySound(WAVFile, 0&, SND_ASYNCOr SND_FILENAME)<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
Private SubWorksheet_Change(ByVal Target As Range)<o:p></o:p>
<o:p> </o:p>
Threshold = "! ALERT !"<o:p></o:p>
If Range("AZ4").Value = ThresholdThen PlayWAV<o:p></o:p>
<o:p> </o:p>
End Sub<o:p></o:p>



This works for the first cell in the column and I'm assuming I'll to add a loop within it to apply the same to the rest of the column cells. I would also like to reference .wav files in other files but using c:\BERT\BEAM-ME-UP-SCOTTY doesn't work? How do I do this and is it something to do with the FILEPATH? This is my first macro and I would appreciate help in achieving this, please help. Many thanks J
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You don't need a loop if you are manually selecting the cells in column AZ.
I believe something like this would work:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lr As Long, rng As Range, c As Range
lr = ActiveSheet.Cells(Rows.Count, "AZ").End(xlUp).Row
Set rng = ActiveSheet.Range("AZ4:AZ" & lr)
Set c = Intersect(Target, rng)
Threshold = "! ALERT !"
If c.Value = Threshold Then PlayWAV
End Sub

This would replace the current worksheet_change code
 
Upvote 0
To use the sound system in other worksheets, you need to make some of the code public instead of private. Put this at the top of code module1.
Code:
Public DeclareFunction PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszNameAs String, _
ByVal hModule As Long, ByVal dwFlags As Long)As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
Then for each .wav file you call you will need to revise this by change the parts highlighted in red.
Code:
Sub [COLOR=#800000]PlayWAV[/COLOR]()
WAVFile = [COLOR=#800000]ThisWorkbook.Path &"\Windows Exclamation.wav"
[/COLOR]Call PlaySound(WAVFile, 0&, SND_ASYNCOr SND_FILENAME)
End Sub
 
Upvote 0
I should also mention that the Play sub goes into the standard module 1. But, your calling code can go anywhere to call the various Play subs.
 
Upvote 0

Forum statistics

Threads
1,203,605
Messages
6,056,258
Members
444,853
Latest member
sam69

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top