How to exec a macro every time a value in a cell change?

earp_

Active Member
Joined
Apr 30, 2008
Messages
305
Hi,
thanks again for your help. I have another question.
Right now my macro change every second in this way...
Sub Auto_Open()
Application.OnTime Now, "myMacro"
End Sub

Sub BAuto_Close()
Application.OnTime mdNextTime1, "myMacro", , False
End Sub

Sub usaShares()
......
......
mdNextTime1 = Now + TimeValue("00:00:01")
Application.OnTime mdNextTime1, "myMacro"

Application.ScreenUpdating = True

End Sub
Is it possible to run myMacro everytime the value in B2 of sheet1 changes?
the value in B2 is in this format
Thanks in advance...
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Perhaps use a worksheet_selectionchange event, e.g.:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("B2")) Is Nothing Then
    MyMacro
End If

End Sub

Applied to the sheet of interest of course.

HTH

Jon
 
Upvote 0
Perhaps use a worksheet_selectionchange event, e.g.:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("B2")) Is Nothing Then
    MyMacro
End If

End Sub
Applied to the sheet of interest of course.

HTH

Jon

Do I need to put the private sub in my module or in ThisWorkBook ?
What is target?
 
Upvote 0
I tried to put the code into Sheet1 and it doesn't work.
I tried also with ThisWorkbok but it doesn't work either.
:(
 
Upvote 0
I found this one
Private Sub Worksheet_Change(ByVal Target As Range)
Static oldVal As Variant
If Intersect(Target, Union(Sheet1.Range("B2"), Sheet1.Range("B2").Precedents)) Is Nothing Then Exit Sub
If [B3] = oldVal Then Exit Sub

oldVal = Sheet1.Range("B2")
'The value changed. Do your stuff
MsgBox "B2 changed!"
End Sub
but where shul i put it?
 
Upvote 0
I tried to put the code into Sheet1 and it doesn't work.
I tried also with ThisWorkbok but it doesn't work either.
:(

What triggers a value change in B2? Recalculation? Perhaps you need to trigger your macro from the calculate event:

Code:
Private Sub Worksheet_Calculate()
    MyMacro
End Sub

Which needs to be added to the appropriate sheet (presume Sheet1)

HTH
Jon
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top