Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable and enable macros on the fly?

I would like to test an Excel VBA app I made.
However the VBA code messes around with the visibility of cells and that's a pest when editing the sheet.

Is there an option is enable and disable macro's on the fly without having to

Close the sheet
Change the macro settings
Reopen the sheet
Close the sheet
Change the macro settings.
etc.
like image 978
Johan Avatar asked Sep 28 '11 07:09

Johan


3 Answers

As far as I know, you can't enable / disable macros from an opened workbook on the fly.
Yet, you shouldn't have to because macros are only triggered thanks to a user click.

The only case I would see is for the Event Procedures (Worksheet_Change or else).
You could then create procedures to activate / deactivate events and call them from buttons in your worksbook:

Sub enableEvents()
    Application.EnableEvents = True
End Sub

Sub disableEvents()
    Application.EnableEvents = False
End Sub

You can also try these tips from Chris Pearson website using global vars you would change depending on your needs:

Public AbortChangeEvent As Boolean

And check if afterwards:

Private Sub Worksheet_Change(ByVal Target As Range)
    If AbortChangeEvent = True Then
        Exit Sub
    End If
    '
    ' rest of code here
    '
End Sub
like image 98
JMax Avatar answered Sep 22 '22 13:09

JMax


To disable macros on the fly, use "Application.EnableEvents = False" via the Immediate window in the VBA editor (and "Application.EnableEvents = True" to turn them back on).

like image 22
T.J.L. Avatar answered Sep 22 '22 13:09

T.J.L.


You can also hold down SHIFT when you open a document to disable macros.

like image 40
JohannesGbg Avatar answered Sep 18 '22 13:09

JohannesGbg