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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
Members
448,989
Latest member
mariah3

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