Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 4
Default 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 !

Last edited by acwai : April 13th 12 at 04:06 AM
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default 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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default 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),"")

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call Statement caldog Excel Programming 4 January 11th 10 11:28 PM
Using the Call Statement caldog Excel Programming 2 January 9th 10 02:59 PM
Variable in call statement donwb Excel Programming 12 April 11th 09 10:18 PM
Call Statement and Return Value [email protected] Excel Programming 3 September 28th 05 08:56 PM
Can you call a macro as in an IF statement kls[_2_] Excel Programming 2 September 11th 04 10:48 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"