Excel/Altering VBA

Advertisement


Question
QUESTION: Hi Bob

I wish to alter the following VBA so that it plays a specific .wav file when the criteria is met.  If this is something that can be done could you help me with it please.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("D2:D4")) Is Nothing Then
With Target
If .Value = 2 Then Beep
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub


Thanks in advance

Chris Mitchell

ANSWER: This is using my path - change as appropriate:

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


Sub PlaySound()
   If Application.CanPlaySounds Then
       'Substitute the path and filename of the sound you want to play
       Call sndPlaySound32("C:\Users\rumlas\Documents\Excel\pacelle\5.wav", 0)
   End If
End Sub

---------- FOLLOW-UP ----------

QUESTION: Hi Bob

I don't quite understand.  Do I merge the two pieces of code and place it into the workbook?

VBA is not really my strength

Answer
Here's all of it. You still have to substitute the path and filename of the sound you want to play inside the Sub PlaySound.

Right-click the sheet tab, select View Code, put in this part (same as yours but I replaced the "beep":

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
  On Error GoTo ws_exit:
  Application.EnableEvents = False
  If Not Intersect(Target, Me.Range("D2:D4")) Is Nothing Then
     If Target.Value = 2 Then PlaySound
  End If

ws_exit:
  Application.EnableEvents = True
End Sub

'=============================
Put this in a regular module:
'=============================

Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" _
      (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Sub PlaySound()
   If Application.CanPlaySounds Then
       'Substitute the path and filename of the sound you want to play
       Call sndPlaySound32("C:\Users\rumlas\Documents\Excel\pacelle\5.wav", 0)
   End If
End Sub

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.