Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change Sounds in Excel VBA

Posted on 2011-03-01
13
Medium Priority
?
360 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

730 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