The VBA Beep function lets you play a system beep sound whenever you like. This example macro shows you how to play a beep whenever an error is encountered in your code. VBA Beep usually comes through your speakers so make sure your volume is up.
VBA Beep and Display Message on Error
Sub SystemBeep() Dim i As Integer On Error GoTo errorhandle: i = "test" 'you can't assign a string to an integer! Exit Sub errorhandle: Application.EnableSound = True Beep MsgBox "Error #" & Err.Number & ": " & Err.Description End Sub
Make powerful macros with our free VBA Developer Kit There’s a lot to unpack here. To save time and become really good at VBA, make sure you get our free VBA Developer Kit below. It’s full of tips and pre-built macros to make writing VBA easier.
There’s a lot to unpack here. To save time and become really good at VBA, make sure you get our free VBA Developer Kit below. It’s full of tips and pre-built macros to make writing VBA easier.
This macro beeps and displays an error because I tried to assign a string to an integer data type. The
Application.EnableSound property ensures sound is enabled for Microsoft Office. Rarely is this necessary so you can often omit that line. Try it without the line if you get an error. It’s common to omit the line when playing system beeps outside of Excel, like in Microsoft Word.
This is just the tip of the iceberg. Soon, I’ll show demonstrate other VBA audio tricks, like how to get your computer to speak to you and how to control your volume with VBA. Stick around!
That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.
Oh, and if you have a question, post it in our VBA Q&A community.
The best free VBA training on the web
I see people struggling with Excel every day and I want to help. That's why I'm giving away my 90-days to Master VBA eCourse and my entire personal macro library for free.