=if(A1>100,"RUN MACRO") What is the command for "RUN MACRO"


Posted by Phil on June 09, 2001 11:39 PM

This is got to be the most simple of questions. What is the excel command to run a macro from a cell when the condition of the cell is true? I have spent 3hours looking for the answer to this most basic question. Please ohhhhh please let me in on the secret :(
Thank you soooo much in advance.
Phil

Posted by Damon Ostrander on June 10, 2001 3:41 PM

Phil,

The reason why you couldn't find it is that it doesn't exist, at least not in this form. Don't forget that =IF(A1>100,expression1,expression2) must return a value for both cases, true and false. Thus a statement equivalent to "RUN MACRO" that doesn't yield anything is not allowed.

That being said, there is a simple way around this. Simply write a function that calls the macro you want to run. Say for example you want to run the macro "OodlesOfCalculations". You can call this by using the formula

=IF(A1>100,Oodles,"")

where you have a function Oodles in a Macro Module something like

Function Oodles()
' set value of function to be returned
Oodles = "Oodles of Calculations Done"

' call macro
OodlesOfCalculations

End Function

This function will be run whenever (and only when) A1 changes AND A1 > 100, and will always return the string value "Oodles of Calculations Done".

Now for the big warning, and the reason why Excel does not provide this capability by more straightforward means:

You cannot use this macro to modify other cells in your workbook. The reason for this is fairly obvious if you think about it. To allow you to do that would permit you to create all kinds of circular references and nasty conflicts that Excel could not resolve because they are implemented in VBA beyond Excel's reach. You can do many useful things with such a macro, like writing data from cells to a file, modifying workbook properties, etc. If you do want this macro to be able to modify worksheets, then you must take a different approach: using the Worksheet_Change event, and trapping a change to cell A1 (using the Target argument), testing if it is greater than 100, and calling the macro if it is. If you do this you are on your own as to handling circular references, etc., but if you are careful it can be very useful and gives you the functionality you are seeking.

Good luck.

Damon




Posted by Phil on June 10, 2001 5:02 PM


Fantastic!!!! It worked with a slight correction
=IF(A1>100,Oodles(),"") instead of =IF(A1>100,Oodles,"")
FYI: I am using the function to trigger an audio alarm when certain conditions are true.

Very good information and great solution,
THANK YOU SO MUCH DAMON,
Phil