Disable a Macro

Canoer

New Member
Joined
Aug 4, 2023
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Is it possible to disable a macro with another macro?

I have a worksheet with "Button A" and a hyperlink that runs "Macro A". Once it is clicked and Macro A runs, I want that hyperlink or that macro disabled until "Button B" is clicked that runs "Macro B". Conversely, once Macro B is run, I want Macro B to be deactivated until Macro A is run again.

The reason is, most times only Macro A should be run, but if it is, it should not be run again, unless Macro B is run, and Macro B should not be run until Macro A has already been run.

Any help would be appreciated. Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
macro A should end with opening the hyperlink, after putting a value in a cell. say A
macro A cannot run again with A in that cell.
macro B cannot run unless A is in that cell and will alter/remove the value, so macro A can run again.
 
Upvote 0
I don't think you can "disable one macro" while allowing others to run in a single workbook. All macros are enabled, or neither none are.

However, there may be other ways to accomplish what you want. You could have one macro update some cell somewhere in your workbook, and then have the other macro check that value before running the code below it.

For example, let's say that you one macro updates the cell Z1 to say "Do Not Run".
Then, you could have code at the beginning of the other macro that looks something like this:
VBA Code:
If Range("Z1") = "Do Not Run" Then Exit Sub
so you exit the code at that point without running anything below that line.

I think doing something like that should accomplish what you are ultimately trying to do.
 
Upvote 0
Solution
Hello,

One way to do it would be to create a named variable, for example "CanRunMA", and define it on False when MacroA is run.

At the beginning of the macro you update its value.



It could look like :

VBA Code:
Sub MacroA()

If Not [CanRunMA].Value2 Then Exit Sub

' … your code A

[CanRunMA].Value2 = False ' macroA did run, it can not be called again

End Sub



Sub MacroB()

If [CanRunMA].Value2 Then Exit Sub

' … your code B

[CanRunMA].Value2 = True ' macroB did run, it can not be called again

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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