Play a sound after sheet calculation

Craig92604

Well-known Member
Joined
Nov 7, 2002
Messages
505
I have a spreadsheet that looks like a slot machine. When I hold down F9 key is there a macro that can play a sound after each calulation kinda like a real slot machine? I am using conditional formating to change the colours
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,432
Hi

Copy the following code into the Worksheet Calculate event :

Code:
Option Explicit

Private Declare Function GetAsyncKeyState Lib "user32" _
    (ByVal vKey As Long) As Integer

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

Private Const VK_F9 = &H78

Private Sub Worksheet_Calculate()

If GetAsyncKeyState(VK_F9) < 0 Then
    sndPlaySound "C:\WINDOWS\Media\tada.wav", 0
End If

End Sub

Change the file path and sound file name to the actual location and name of the sound file you want to play. If you want the code to stop while it plays the sound keep the code as is, but if you want the code to continue processing while playing the sound then change the ',0' part to ',1' (without the quotes).

HTH, Andrew

P.S. Credit to Tom Urtis in this thread for the key press routine.
 

Craig92604

Well-known Member
Joined
Nov 7, 2002
Messages
505
Not sure what I am doing wrong with this code. When I run it I get the following error "

Compile error:

Only comments may appear after end Sub, End Function, or end property?
Any help would be appreciatted


Private Sub Worksheet_Calculate()
Option Explicit

Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer

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

Private Const VK_F9 = &H78

Private Sub Worksheet_Calculate()

If GetAsyncKeyState(VK_F9) < 0 Then
sndPlaySound "C:\WINDOWS\Media\tada.wav", 0
End If

End Sub
 

sam slade

New Member
Joined
Sep 14, 2006
Messages
22
Craig,

you don't need the 1st Private Sub Worksheet_Calculate()

That should make it work.

Sam
 

Forum statistics

Threads
1,141,720
Messages
5,708,081
Members
421,545
Latest member
TWR

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