Run macro based on cell value in worksheet

L

LB79

Does anyone know of a way to do run a macro based on a cell value. EG i
cell A1 = 0, dont run macro - otherwise run macro
 
R

Romanian37

Does anyone know of a way to do run a macro based on a cell value. EG i
cell A1 = 0, dont run macro - otherwise run macro.


below macro will run your code is cell a1 is anything but 0.
Do you want the macro to run automatically if a different value i
inputted into cell a1, or are you happy for the user to run the macr
manually? (if automatically, more code is required)



sub Macro 1 ()

if cells(1,1) = 0 then exit sub

'your code here


end su
 
J

JE McGimpsey

When should A1 be evaluated? When an entry is made? When a calculation
is performed? Every 10 seconds?

If when an entry is made in A1, put something like this in your
worksheet code module (right-click the worksheet tab and choose View
Code):

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Count > 1 Then Exit Sub
If .Address(False, False) = "A1" Then
If IsNumeric(.Value) Then If .Value <> 0 Then MyMacro
End With
End Sub

where MyMacro is the name of your macro.


If when a calculation is performed, put something like this in the
worksheet code module instead:

Private Sub Worksheet_Calculate()
With Range("A1")
If IsNumeric(.Value) Then If .Value <> 0 Then MyMacro
End With
End Sub
 
Top