Solved

Change Sounds in Excel VBA

Posted on 2011-03-01
13
356 Views
Last Modified: 2012-08-13
Hello Experts,

Should be fairly straight forward.....

Can someone please show me how to change the sounds in Excel VBA.

At the moment, I have the following lines of code in excel and I would like to change the sound to one of the sounds in windows, e.g chime or ding, instead of just 'beep'

Option Explicit

Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

If xLastRow < 2 Then Exit Sub


For Each xCell In Range("O2:O" & xLastRow)
    xValue = xCell.Value
    If Not IsError(xValue) Then
        If xValue <> "" And xValue = 0.0002 Then
            Beep
            Cells(1, 15).Interior.Color = 255
            Point02 = True
            Exit For
        End If
    End If
Next



If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
    xValue = xCell.Value
    xValue2 = xCell.Offset(0, -13).Value
    If Not IsError(xValue) And Not IsError(xValue2) Then
        If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then
            Beep
            Cells(1, 17).Interior.Color = 255
            Exit Sub
        End If
    End If
Next



Cheers


Carlton
0
Comment
Question by:cpatte7372
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 4

Expert Comment

by:loki_loki
ID: 35006280
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35006341
Alternatively you could try something with specific data like speaking the cell:

Add the code to a normal code module then modify your code to:

Private Sub Worksheet_Calculate()
Dim xCell As Range
Dim xLastRow As Long
Dim xValue As Variant
Dim xValue2 As Variant
Dim Point02 As Boolean

xLastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

If xLastRow < 2 Then Exit Sub


For Each xCell In Range("O2:O" & xLastRow)
    xValue = xCell.Value
    If Not IsError(xValue) Then
        If xValue <> "" And xValue = 0.0002 Then
            Speech1 xCell.Address(False, False)
            Cells(1, 15).Interior.Color = 255
            Point02 = True
            Exit For
        End If
    End If
Next



If Not Point02 Then Cells(1, 15).Interior.Color = 65535

For Each xCell In Range("q2:q" & xLastRow)
    xValue = xCell.Value
    xValue2 = xCell.Offset(0, -13).Value
    If Not IsError(xValue) And Not IsError(xValue2) Then
        If xValue <> "" And xValue2 <> "" And Round(xValue, 2) = Round(xValue2, 2) Then
            Speech1 xCell.Address(False, False)
            Cells(1, 17).Interior.Color = 255
            Exit Sub
        End If
    End If
Next
End Sub


Chris
Sub Speech1(str As String)
Dim objVoice As Object
Dim strVoice As Variant
Dim intVoice As Integer
Const WAIT_INFINITE = -1
Const SVSFDefault = 0
Const SVSFlagsAsync = 1
Const SVSFPurgeBeforeSpeak = 2
Const SVSFIsXML = 8
Const SVSFIsNotXML = 16
Const SVSFNLPSpeakPunc = 64
    
    Set objVoice = CreateObject("SAPI.SpVoice")
    objVoice.Speak str, SVSFlagsAsync + SVSFPurgeBeforeSpeak + SVSFNLPSpeakPunc
    Do
        DoEvents
    Loop Until objVoice.WaitUntilDone(10)
    
    Set objVoice = Nothing

End Sub

Open in new window

0
 

Author Comment

by:cpatte7372
ID: 35006586
Thanks for responding,

I'm going to check out the link, in the meantime, Chris I'm going to give your code ago..


Back in a bit.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:cpatte7372
ID: 35006724
Ha ha, Chris that sound freaked me out.......

Anyway, I'm trying to use the example from the link provided by loki. I've applied the statement as suggested but each time I run it I keep on getting a compile error: which states that Constants, fixed-length strings, arrays, user-defined types Declare statements not allowed as Public members of object modules.

Do any of you guys understand?

Cheers
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35006776
Did you place the library definitiaon at the top of the code module ... and are you using a normal code module?

Chris
0
 

Author Comment

by:cpatte7372
ID: 35006777
Just figured it out.

I have to change 'Public' to 'Private'

Cheers
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35006787
BTW ... by normal code module I mean module1 etc as inserted via "insert | Module"

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 35006809
Nah

That is a restriction, and against custom and practice, (event modules should be limited to events).  Place the library def and the sound test sub in a normal code module and call them that way from your sheet module ... and keep the sub def for the sound as public

Chris
0
 

Author Comment

by:cpatte7372
ID: 35006918
Chris,

Not sure what you mean. Can you please illustrate. I have attached the trading xlsm

Cheers

BTW, you won't see any values because
Trade-Day--10-Experts.xlsm
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 35006995
MOdified for the sub in a normal code module

Chris
Trade-Day--10-Experts.xlsm
0
 

Author Comment

by:cpatte7372
ID: 35008519
Cheers Chris,

Will check it out after markets have closed.

Cheers
0
 

Author Comment

by:cpatte7372
ID: 35009104
Chris that works fine.

Cheers mate.
0
 

Author Closing Comment

by:cpatte7372
ID: 35009123
Cheers
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question