What's wrong with this code?

ExcelFailure

Board Regular
Joined
Mar 6, 2013
Messages
74
Hello and thanks,

Does anyone know what is wrong with this code? It comes up with a compile error.

Thanks again

Code:
Private Declare Function sndPlaySound Lib "winmm.dll" _
   Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long


Private Sub Worksheet_calculate(ByVal Target As Range)
   Dim KeyCells               As Range


   ' The variable KeyCells contains the cells that will
   ' cause an alert when they are changed.
   Set KeyCells = Range("I2")


   If Not Application.Intersect(KeyCells, Target) _
      Is Nothing Then
      PlayWavFile "c:\tada.wav", False
   End If


End Sub
Sub PlayWavFile(WavFileName As String, Wait As Boolean)
    If Dir(WavFileName) = "" Then Exit Sub ' no file to play
    If Wait Then ' play sound before running any more code
        sndPlaySound WavFileName, 0
    Else ' play sound while code is running
        sndPlaySound WavFileName, 1
   End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Worksheet_Calculate does not have Target as an argument. It is simply

Code:
Private Sub Worksheet_Calculate()
'code here
End Sub
 
Upvote 0
Hi and thanks for the reply. It's now coming up with a run time error and highllighting the following piece of code:

Code:
If Not Application.Intersect(KeyCells, Target) _
      Is Nothing Then

Any ideas? Thanks again
 
Upvote 0
You would need something like

Code:
Private Sub Worksheet_Calculate()
Static oldval As Variant
If Range("I2").Value <> oldval Then
    oldval = Range("I2").Value
    PlayWavFile "c:\tada.wav", False
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,759
Messages
6,057,191
Members
444,913
Latest member
ILGSE

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