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
Copy the following code into the Worksheet Calculate event :
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
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).
P.S. Credit to Tom Urtis in this thread for the key press routine.