Running a macro depending on a cell value

  • Thread starter Thread starter Brian Ferris
  • Start date Start date
B

Brian Ferris

Hi there,

I have an interesting one here ...

I have assigned a macro to a command button. However I
want to control this macro not to happen, i.e. not to run
on click of the command button, depending on the value of
another cell.

That is say cell a1 = 1

I want the macro to work on click of command_button1 if
cell a1 = 1. But i do not want it to run, i.e. act as if
there is no macro assigned to it, if cell a1 = 2 or any
other number not 1.

Can this be done.

Would appreciate any suggestions

Thanks in advance,
Brian
 
Sure you can do that with an If statement. Example:

Sub Button2_Click()
If [A1].Value <> 1 Then
Exit Sub
Else
MsgBox "dog"
End If
End Sub

The above code was attached to a button. It will run (i.e., display a
message box containing the message "dog") only if cell A1 contains a value
of 1. Else it simply quits without doing anything.

MRO
 
Hi,



Put this code at the very begining of your macro »



If Range("a1").Value <> 1 Then Exit Sub
 
Back
Top