ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   If, then: to run macro (https://www.excelbanter.com/excel-worksheet-functions/139606-if-then-run-macro.html)

Sandy

If, then: to run macro
 
Hi

Is it possible to have a macro run when a certain condition is met on a
worksheet (or between worksheets).

Example:

If CellA1 on sheet1 < CellA1 on sheet4 then run the macro "Evaluate"
otherwise do nothing

Thanks

Sandy



Bob Phillips

If, then: to run macro
 
Using event code


Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1" '<== change to suit

On Error GoTo ws_exit
Application.EnableEvents = False

If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
If .Value < "Worksheets("Shete4").Range("A1").Value Then
' do your stuff
End If
End With
End If

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Sandy" wrote in message
...
Hi

Is it possible to have a macro run when a certain condition is met on a
worksheet (or between worksheets).

Example:

If CellA1 on sheet1 < CellA1 on sheet4 then run the macro "Evaluate"
otherwise do nothing

Thanks

Sandy




Gary''s Student

If, then: to run macro
 
It is possible to do this if the contents on the cells are defined by data
entry rather than calculation. The macro is called an Event Macro. For
information see:

http://www.cpearson.com/excel/excelM.htm#RunFromCell

You can use the calculate event if the cells are defined by calculations,
but you need to be careful to avoid infinite loops.
--
Gary''s Student - gsnu200716


"Sandy" wrote:

Hi

Is it possible to have a macro run when a certain condition is met on a
worksheet (or between worksheets).

Example:

If CellA1 on sheet1 < CellA1 on sheet4 then run the macro "Evaluate"
otherwise do nothing

Thanks

Sandy




Sandy

If, then: to run macro
 
Thank you both - works fine

Sandy

"Sandy" wrote in message
...
Hi

Is it possible to have a macro run when a certain condition is met on a
worksheet (or between worksheets).

Example:

If CellA1 on sheet1 < CellA1 on sheet4 then run the macro "Evaluate"
otherwise do nothing

Thanks

Sandy





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

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