0

He all I need help writing an If statement that will run a macro every time the value in cell D8 is over one. No idea where to start

David Van der Vieren
  • 265
  • 4
  • 11
  • 25
  • Look at this thread for automatically running code when the value of a cell changes http://stackoverflow.com/questions/409434/automatically-execute-an-excel-macro-on-a-cell-change – JustinJDavies Mar 28 '13 at 20:31
  • Are you sure you need a macro? What do you need it to do if your trigger cell is > 1? – PowerUser Mar 28 '13 at 22:36

2 Answers2

1

Here you go...

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("D1")) Is Nothing Then Exit Sub
If Not Range("D1").Value > 1 Then Exit Sub

MsgBox "D1 > 0"

End Sub

For more info on the _Change event:

http://msdn.microsoft.com/en-us/library/office/ff839775.aspx

David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • That is not working unfortunatly. the first If statement is giving me a run time error 424 – David Van der Vieren Mar 28 '13 at 20:37
  • 2
    1: Check for typos. 2. Make sure you have put this in the Worksheet's code module, NOT in an ordinary code module. 3. If you still can't get it to work, debug the value of `Target`. – David Zemens Mar 28 '13 at 21:06
0

It is a simple if statement.

    If Range("D8").Value > 1 Then
        '~~> Your code here
    End If

You can put that in the Worksheet_Change event. I would also recommend reading this link which talks about Worksheet_Change

If the value in Range("D8") is changing because of a formula then you might have to use the _Calculate event. See this link for an example.

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250