Play Sound in Excel Automatically Using VBA Macro

Опубликовано: 13 Октябрь 2024
на канале: Dinesh Kumar Takyar
17,125
207

How to play a sound in Excel with a macro or VBA code to warn us, for example, of a wrong data entry. More details available on our website: https://www.exceltrainingvideos.com/p...

Here's the complete macro code:
Option Explicit

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

Private Sub Worksheet_Change(ByVal Target As Range)
Dim highestGST As Long

highestGST = 28

If Range("B2").Value GREATER THAN highestGST Then

Call sndPlaySound32("C:\windows\media\Windows Error.wav", 1)

End If

End Sub

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

Sub PlaySound()
Dim lastrow As Long, i As Long

lastrow = Application.WorksheetFunction.CountA(Sheet1.Range("B:B"))

MsgBox lastrow

For i = 2 To lastrow
If Cells(i, 2) GREATER THAN 28 Then Call sndPlaySound32("C:\Windows\Media\Windows Exclamation.wav", &H8)
Next i

End Sub

NOTE: Replace GREATER THAN with an angular sign.