ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   can I use IF statement to call a VBA function ? (https://www.excelbanter.com/excel-programming/445760-can-i-use-if-statement-call-vba-function.html)

acwai

can I use IF statement to call a VBA function ?
 
I have used the code in here to create a function called Alarm

http://spreadsheetpage.com/index.php...a_cells_value/

I can get it to play a sound by using =alarm(a1,"2") to test whether a1 is 2

Can I use an IF statement to trigger the function alarm ?

Thanks !

merjet

can I use IF statement to call a VBA function ?
 
Yes, but I wonder why you would want that when the function has an IF statement within already.


joeu2004[_2_]

can I use IF statement to call a VBA function ?
 
"acwai" wrote:
I have used the code in here to create a function
called Alarm http://tinyurl.com/c59tec9
I can get it to play a sound by using =alarm(a1,"2")
to test whether a1 is 2


What a silly thing to do! Not you; the design of the function.


"acwai" wrote:
Can I use an IF statement to trigger the function alarm ?


Redesign function Alarm as follows:

'Windows API function declaration
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Function Alarm(Optional retnval = "")
Dim WAVFile As String
Const SND_ASYNC = &H1
Const SND_FILENAME = &H20000
On Error GoTo ErrHandler
WAVFile = ThisWorkbook.Path & "\sound.wav" 'Edit this statement
Call PlaySound(WAVFile, 0&, SND_ASYNC Or SND_FILENAME)
ErrHandler:
Alarm = retnval
End Function

Then you can call it with the following equivalent IF expression:

=IF(A12,Alarm(),"")

And of course, this permits you to use Alarm with much more complicated
conditional expressions, for example:

=IF(AND(A12,B1<3,C1=4),Alarm(),"")

By default, Alarm returns the null string (""). The optional parameter
allows you to use Alarm in arithmetic expressions, to wit:

=IF(A12,2+Alarm(0),"")

or more simply:

=IF(A12,Alarm(2),"")



All times are GMT +1. The time now is 09:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com