prevent automatic execution of a macro

macrofan

New Member
Joined
May 29, 2011
Messages
15
I have a command button which is assigned to a macro for a click. However, whenever I open the workbook this macro is automatically executed. But this is not what I want. I want this macro to run only when I click the button. How can I prevent the automatic execution when I open Excel?

Thanks.
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
You can't prevent the automatic execution of the macro when you open Excel but you can prevent the automatic execution of the macro when you open the workbook itself by holding the Shift key down until it's finished opening.

Or if it's a permanent thing, you can modify the Workbook_Open event handler so that it doesn't run the code: press Alt-F11 to get into the VB editor, then Ctrl-R to open the Project Explorer, double-click ThisWorkbook and make the necessary changes. If you don't know how to do this, post the code here (between code tage - the # icon in the advanced editor toolbar) and we'll talk you through it.
 
Last edited:
Upvote 0
Ruddles, thanks for your reply.

Is this what you meant?

Private Sub Workbook_Open()

End Sub

I have a macro with a name of "Test_Click"
 
Upvote 0
Only code in specifically named subroutines in specific modules is executed automatically. If you have a subroutine named XYZ in a standard module, there's no way Excel would automatically run it.

So, what is your subroutine named and what module is it in?

I have a command button which is assigned to a macro for a click. However, whenever I open the workbook this macro is automatically executed. But this is not what I want. I want this macro to run only when I click the button. How can I prevent the automatic execution when I open Excel?

Thanks.
 
Upvote 0
Only code in specifically named subroutines in specific modules is executed automatically. If you have a subroutine named XYZ in a standard module, there's no way Excel would automatically run it.

So, what is your subroutine named and what module is it in?


I am a beginner with VBA, so that I am not sure I get your comment fully.

I placed the "TestButton" module in the general modeule category, should I put in class module?
 
Upvote 0
Private Sub Workbook_Open()

End Sub
If there's nothing between those lines, nothing should run automatically when the workbook is opened. What makes you think something is running?

@Tushar, could it be something in Personal.xls? (I should know more about this but I never use it.)
 
Upvote 0
If there's nothing between those lines, nothing should run automatically when the workbook is opened. What makes you think something is running?

@Tushar, could it be something in Personal.xls? (I should know more about this but I never use it.)

I have this procedure (TestButton) to do some calculation based on original data. Everytime when I open the workbook, the newly calculated columes appear in addition to the columes of original data.
 
Upvote 0
Try this: insert a line at the top of that macro - immediately after the Sub line - as follows:-
Code:
MsgBox "Macro is running"
Run the macro manually to confirm the message box appears.

Now close and re-open the workbook. Do you get the message box?
 
Last edited:
Upvote 0
And if you delete that macro, then save and re-open the workbook, that doesn't happen?

If I delete them, and save and reopen the file,

hah, hah, hah,

the columes disappear, and when I click the button for calculation, they reappear again.

Thank you and Tushar for your time, patience and your help.

You guys have a wonderful long weekend!
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

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