Running a Macro from a Spreadsheet Function

  • Thread starter Thread starter Patrick (GVC)
  • Start date Start date
P

Patrick (GVC)

Does anyone know a method of calling/running a macro from within a cell
function (e.g. a logical IF function)? I am currently using XP Office with
Excel 2002.
 
You can't run a macro from an Excel IF function.

You may be able to run a macro from event code that is triggered by the results
of an IF calculation.

More details would be in order but here is sample sheet event code that runs on
the results of an IF

A1 fomula is =B1 * C1

Private Sub Worksheet_Calculate()
On Error GoTo stoppit
Application.EnableEvents = False
With Me.Range("A1")
If .Value <> 144 Then
MsgBox "Please be advised that A1 does not equal the correct amount."
'Call "your macro name" to be used instead of the msgbox
End If
End With
stoppit:
Application.EnableEvents = True
End Sub


Gord Dibben MS Excel MVP
 
Sat, 9 Feb 2008 16:19:00 -0800 from <=?Utf-8?B?UGF0cmljayAoR1ZDKQ==?
= said:
Does anyone know a method of calling/running a macro from within a cell
function (e.g. a logical IF function)? I am currently using XP Office with
Excel 2002.

Make it a user-defined function. As far as I know, a function can
contain any code that you could put into a macro.

--
Stan Brown, Oak Road Systems, Tompkins County, New York, USA
http://OakRoadSystems.com
A: Maybe because some people are too annoyed by top posting.
Q: Why do I not get an answer to my question(s)?
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top