VBA - Temporarily disable a macro to run another

NBVC

Well-known Member
Joined
Aug 31, 2005
Messages
5,828
I have a spreadsheet that I run a query with.

I have a simple worksheet event macro that forces cell G1 to always be active (that is the input cell for the query to refresh its data based upon)

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("G1").Select
End Sub

I want to be able to temporarily "disable" that macro in order to click a button on the sheet.

Currently it won't let me click that button because of the worksheet event macro forcing cell G1 to be active....

Any suggestions on what I can do?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
maybe put application.enableevents = false as the first line in the button's code?
 

NBVC

Well-known Member
Joined
Aug 31, 2005
Messages
5,828
Yup! That did the trick! I forgot about the Enableevents property...

Thanks for refreshing my memory Jonmo1
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
no problem, just don't forget to reset back to true as the last line of code in the button.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650

ADVERTISEMENT

I may be wrong but I think that EnableEvents resets to True when an End Sub is encountered.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VoG II:
Nope.

Try this:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
MsgBox "Hello"
End Sub

If it reset, it should show Hello at every selection change, but as you can see it doesn't.
 

BmtJedi

New Member
Joined
May 5, 2006
Messages
1
Old thread - but solution still good. Formulas are not pasting down in a table with the onchange events active. Added a button to disable/enable quickly when the table formulas need updating. Minor but annoying problem having to copy formulas manually on every row. Was issue in Excel 2013.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,149
Messages
5,835,683
Members
430,376
Latest member
eeehhhyyy

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
Top