Coding to prevent macro calculations unless on a specific worksheet

spurs

Active Member
Joined
Oct 18, 2006
Messages
479
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
  5. 2003 or older
Platform
  1. Windows
I have a workbook that has many worksheets. One of these worksheets is named "Plot" which graphs data.

Right now, whenever I do an entry on the worksheet "Main", I run a macro called "Update" which does all of the calculations needed and updates the workbook. This is a slow process and takes 2 seconds to do all of the updates.

I suspect that I can save a lot of delay time by not doing updates to calculations associated to plot the graph on worksheet "Plot".

What I would like to do is add code to the "Update" macro that recognizes that I am on the worksheet "Main" and am not looking at other worksheets so I want to prevent doing background calculations to update the graphs. However if I move off of worksheet "Main". or "save" the workbook, I want to make sure that the calculations are done.

What are some of the lines of code that I can use to:
a) recognize that I am on worksheet "Main"
b) trigger the macro "Update" when I leave worksheet "Main"
c) recognize that I am saving the workbook and allow "Update" to also do the graphical calculations
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
a)
Code:
If ActiveSheet.Name = "Main" Then

End If
b) Whenever the focus moves from a worksheet, the Worksheet.Deactivate event is triggered. You can use the Worksheet_Deactivate event handler in the code block for the Main worksheet to do things when the worksheet loses focus. This event will be triggered for lots of reasons, not just activating another sheet; this includes the focus moving to another application or the desktop.
Code:
Private Sub Worksheet_Deactivate()

End Sub
c) Just before the worksheet is saved, the Workbook.BeforeSave event is triggered. Putting a Workbook_BeforeSave handler in the ThisWorkbook code block will let you call update just before you save the workbook.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

End Sub
 
Upvote 0
This is fantastic
thank you
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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