Automatic Macro Trigger - Macro runs multiple times

ryandonovan22

New Member
Joined
Sep 17, 2015
Messages
36
Hello,

I am trying to get my 'TidyAll' Macro to run when a cell is changed and I am using the following code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("H5")) Is Nothing Then TidyAll
End Sub

It is working, although for some reason the macro being called up runs 3 times then stops?

What am I doing wrong?

FYI - The Macro is stored in a module.

Thanks,
Ryan.
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hello,

I am trying to get my 'TidyAll' Macro to run when a cell is changed and I am using the following code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("H5")) Is Nothing Then TidyAll
End Sub

It is working, although for some reason the macro being called up runs 3 times then stops?

What am I doing wrong?

FYI - The Macro is stored in a module.

Thanks,
Ryan.
Is the macro installed as sheet code for the sheet you want to monitor for changes? Does the TidyAll routine make changes to that sheet?
 
Upvote 0
Is the macro installed as sheet code for the sheet you want to monitor for changes? Does the TidyAll routine make changes to that sheet?

To the first question, Yes, I believe so (it auto runs when data is added)

secondly the TidyAll routine adds columns, deletes columns, deletes rows, calculates stuff, and generally cleans up the look, oh a VLOOKUP too.
 
Upvote 0
To the first question, Yes, I believe so (it auto runs when data is added)

secondly the TidyAll routine adds columns, deletes columns, deletes rows, calculates stuff, and generally cleans up the look, oh a VLOOKUP too.
If your doing that on the sheet that you are monitoring, I'm surprised, TidyAll runs only 3 times as opposed to an infinite loop. The addition/deletion of cells are change events which will continuously trigger TidyAll.

Try adding this at the start of the worksheet_change code:

Application.EnableEvents = False

and this just before the end sub line:

Application.EnableEvents = True
 
Upvote 0
If your doing that on the sheet that you are monitoring, I'm surprised, TidyAll runs only 3 times as opposed to an infinite loop. The addition/deletion of cells are change events which will continuously trigger TidyAll.

Try adding this at the start of the worksheet_change code:

Application.EnableEvents = False

and this just before the end sub line:

Application.EnableEvents = True

Seems to have worked perfectly.

Thanks for your help.
 
Upvote 0
You are welcome - thanks for the reply.

No worries, JoeMo.

Just a heads up, it is working fine. For some reason Excel just stopped running the macro but I think it was just excel being excel and when I closed and reopened all of the files it worked fine.

Ryan
 
Upvote 0
No worries, JoeMo.

Just a heads up, it is working fine. For some reason Excel just stopped running the macro but I think it was just excel being excel and when I closed and reopened all of the files it worked fine.

Ryan
Possibly you ran it through the line that disables events but it never got to the line at the end that enables them again. Or, maybe you moved the line that enables events to your other sub. If that should happen again, open the VBE window, press ctrl + g to open the Immediate Window, type "Application.EnableEvents = True" (w/o the quote marks) and press Enter. That turns events back on.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,636
Members
449,109
Latest member
Sebas8956

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