How do you play a wav file when a cell value changes?

dwebb03

New Member
Joined
Aug 6, 2003
Messages
22
How can i trigger this code when Cell A1 changes to a value greater 77, or is there another way this is all I could find while searching. Thanx in advance


Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000

Sub PlayWAV()
WAVFile = "hal.wav"
WAVFile = ThisWorkbook.Path & "\" & WAVFile
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
check out the worksheet_change function.

an example':
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "A1" And Target.Value > 77 Then
        MsgBox "testtest"
        Beep
        PlayWAV
    End If

End Sub
 

dwebb03

New Member
Joined
Aug 6, 2003
Messages
22
Question

Where do I put this portion of code I have the other portion in the thisworkbook
 

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
you should put it in the sheet where 'A1' is found (ie. sheet1)
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832

ADVERTISEMENT

Hi,

if I might comment a bit:
If Target.Address = "A1" will not work
Address is returning "$A$1" (absolute reference)
you can use
If Target.Address = "$A$1"
or
If Target.Address(0, 0) = "A1" (relative reference)

see helpfiles for "Address"

kind regards,
Erik
 

dwebb03

New Member
Joined
Aug 6, 2003
Messages
22
Help Please

It's coming up with sub or function not described when it comes to PlayWAV Do I have the first portion of code correct and in the right sheet Thank you.
 

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832

ADVERTISEMENT

playWAV must be in a normal module

On the Insert menu in the VBE, click Module. (if necessary)
In the module (the white area at the right), paste the your code
 

dwebb03

New Member
Joined
Aug 6, 2003
Messages
22
A little more help please

Thanx Erik I'm Getting closer but it only works when the "A1" value is above 77 and I click the cell. Can you help me so that when ever the value is above 77 it plays the wav automatically perhaps repeating if I'm not asking too much. Thank You I'm quite new at this as you can tell.
 

Harvey

Well-known Member
Joined
Nov 18, 2004
Messages
953
is A1 a formula?
if that is the case you have to change worksheet_change into worksheet_calculate
 

dwebb03

New Member
Joined
Aug 6, 2003
Messages
22
Thanx still trying

yes eventually but I was just trying to learn how to get it to work with just changing the value in the cell manually. I changed it to calculate but that doesn't seem to work.
 

Forum statistics

Threads
1,141,630
Messages
5,707,520
Members
421,512
Latest member
jc364698

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
Top