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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
maybe put application.enableevents = false as the first line in the button's code?
 
Upvote 0
Yup! That did the trick! I forgot about the Enableevents property...

Thanks for refreshing my memory Jonmo1
 
Upvote 0
no problem, just don't forget to reset back to true as the last line of code in the button.
 
Upvote 0
I may be wrong but I think that EnableEvents resets to True when an End Sub is encountered.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

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