ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Run Macro based on IF (https://www.excelbanter.com/excel-programming/400405-run-macro-based-if.html)

Nikki

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

Bob Phillips

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




Don Guillett

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



Don Guillett

Run Macro based on IF
 
As previously written it would fire on any change. Use this way if only a1
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address < "$A$1" Then Exit Sub
If Target < Range("b1") Then Call mymacro
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Don Guillett" wrote in message
...
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




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