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
Solved

Change Sounds in Excel VBA

Posted on 2011-03-01
13
352 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

792 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