Macro that automatically executes when cell value changes

Koani

New Member
Joined
May 10, 2010
Messages
2
I am trying to have a specified macro run whenever cell B19 equals one. I tried using Worksheet_Calculate but the problem is that when the macro is run, it changes cells (other than B19) which causes the macro to start over. (One of the first actions of the macro is to enter a new row at 25)

Thoughts?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Welcome to the Board!

Place an IF statement in your code to limit it to just that cell, i.e.

Code:
If Target.Address=Range("B19").Address Then
...
 
Upvote 0
Welcome to the Board!

If B19 is the result of a calculation then the Calculate event is what you want.

If the code is going into a recursive loop you can add Application.EnableEvents = False at the beginning of the code, just make sure to set it back to True at the end.

HTH,
 
Upvote 0
Try something like

Code:
Private Sub Worksheet_Calculate()
If Range("B19").Value = 1 Then
    Application.EnableEvents = False
    Call MyMacro
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Not Application.Intersect(Target, Range("B19")) Is Nothing Then
        If Range("B19").Value = 1
        Call your_macro
        Exit Sub
        End IF
    End If
End Sub
 
Upvote 0
Scratch my reply. Using TARGET works well with the Worksheet_Change event, but I don't think it applies here with the Worksheet_Calculate event (time to get those eyes checked! :roll:).
 
Upvote 0
Thank you so much for all your help! I've been able to get the file to work as long as I have the worksheet open. Is there a way so that the macro can execute in the background (if I I'm not viewing the specific worksheet, or workbook - the target workbook will be open)?
 
Upvote 0
You'd have to put code in whatever workbook you're in and tell it to run code in the other one. Event code isn't going to run in a deactivated workbook.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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