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?
 

Some videos you may like

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,519
Messages
5,596,635
Members
414,083
Latest member
Mrsash

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