Solved

Change Sounds in Excel VBA

Posted on 2011-03-01
13
351 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

803 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