Run Macro based on IF

G

Guest

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!
 
B

Bob Phillips

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)
 
D

Don Guillett

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
 
D

Don Guillett

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
(e-mail address removed)
Don Guillett said:
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
 
G

Guest

Thank you Bob-
It worked as I hoped this time.
--
Thanks,
Nikki


Bob Phillips said:
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)
 

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

Top