Creating a VBA macro to play frequency and duration

I'm trying to figure out how to write a VBA macro for Excel that will play a beep of a specified frequency for a specifed duration.

 

I've done similar programming in BASIC and TurboPascal with Sound, NoSound, and Delay statements. In terms of VisualBasic, I've only found documentation pertaining to System.Console.Beep(frequency, duration). However, this doesn't seem to work when I try to apply it in VBA for Excel.

 

Thanks.

 

Answer
Answer

Insert the following declaration at the top of a module:

 

Public Declare Function Beep Lib "kernel32" _
   (ByVal dwFreq As Long, _
    ByVal dwDuration As Long) As Long

 

You can now use code like this to play a beep of 880 Hz during 300 milliseconds:

 

Beep 880, 300

---
Kind regards, HansV
https://www.eileenslounge.com

14 people were helped by this reply

·

Did this solve your problem?

Sorry this didn't help.

Great! Thanks for marking this as the answer.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this response?

Thanks for your feedback.

 
 

Question Info


Last updated July 4, 2020 Views 9,821 Applies to: