Solved

Change Sounds in Excel VBA

Posted on 2011-03-01
13
346 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now