Run Macro based on IF
I want to run a macro if cell a1 does not equal b1.
Private Sub Worksheet_Change(ByVal Target As Range) If (a1 < b1) Then Macro1 End If End Sub When I use an equal sign the macro runs every time and when I use the not equal it won't run at all. Thanks for the help! -- Thanks, Nikki |
Run Macro based on IF
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "A1:B1" '<== change to suit On Error GoTo ws_exit Application.EnableEvents = False If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then If Me.Range("A1").Value < Me.Range("B1").Value Then Call macro1 End If End If ws_exit: Application.EnableEvents = True End Sub -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Nikki" wrote in message ... I want to run a macro if cell a1 does not equal b1. Private Sub Worksheet_Change(ByVal Target As Range) If (a1 < b1) Then Macro1 End If End Sub When I use an equal sign the macro runs every time and when I use the not equal it won't run at all. Thanks for the help! -- Thanks, Nikki |
Run Macro based on IF
You would use a worksheet change if you are changing a1 or b1
Private Sub Worksheet_Change(ByVal Target As Range) If Target < Range("b1") Then Call mymacro 'if? you really want to fire with ANY change use 'If Range("a1") < Range("b1") Then Call mymacro End Sub Sub mymacro() MsgBox "hi" End Sub -- Don Guillett Microsoft MVP Excel SalesAid Software "Nikki" wrote in message ... I want to run a macro if cell a1 does not equal b1. Private Sub Worksheet_Change(ByVal Target As Range) If (a1 < b1) Then Macro1 End If End Sub When I use an equal sign the macro runs every time and when I use the not equal it won't run at all. Thanks for the help! -- Thanks, Nikki |
Run Macro based on IF
Thank you Bob-
It worked as I hoped this time. -- Thanks, Nikki "Bob Phillips" wrote: Private Sub Worksheet_Change(ByVal Target As Range) Const WS_RANGE As String = "A1:B1" '<== change to suit On Error GoTo ws_exit Application.EnableEvents = False If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then If Me.Range("A1").Value < Me.Range("B1").Value Then Call macro1 End If End If ws_exit: Application.EnableEvents = True End Sub -- HTH Bob (there's no email, no snail mail, but somewhere should be gmail in my addy) "Nikki" wrote in message ... I want to run a macro if cell a1 does not equal b1. Private Sub Worksheet_Change(ByVal Target As Range) If (a1 < b1) Then Macro1 End If End Sub When I use an equal sign the macro runs every time and when I use the not equal it won't run at all. Thanks for the help! -- Thanks, Nikki |
All times are GMT +1. The time now is 12:46 PM. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com