ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   can i use an IF Statement to run a macro? (https://www.excelbanter.com/excel-worksheet-functions/12655-can-i-use-if-statement-run-macro.html)

Davidrowland88

can i use an IF Statement to run a macro?
 
i am using excel 2000. I want to know if you can use an IF Statement to run a
macro. E.G If a cell equals 1 then run a certain macro

JE McGimpsey

No, in general, formulas can only return values to their calling cells.
However, you can use an event macro that will check your cell each time
the sheet is calculated, say, and run the macro if the value of the cell
is 1. Put this in your worksheet code module (right-click the worksheet
tab and choose View Code):

Private Sub Worksheet_Calculate()
If Range("A1").Value = 1 Then MyMacro
End Sub

where MyMacro is the name of your macro.


In article ,
"Davidrowland88" wrote:

i am using excel 2000. I want to know if you can use an IF Statement to run a
macro. E.G If a cell equals 1 then run a certain macro


Ron Rosenfeld

On Fri, 11 Feb 2005 04:25:02 -0800, "Davidrowland88"
wrote:

i am using excel 2000. I want to know if you can use an IF Statement to run a
macro. E.G If a cell equals 1 then run a certain macro


By IF statement I assume you mean the IF worksheet function; and not an IF
statement within the macro itself.

Probably not as a worksheet function cannot change another cell.

However, you could set up your macro as an event-triggered macro, and within
the macro test to see if the cell equals 1.


--ron


All times are GMT +1. The time now is 10:03 PM.

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