VBA fails to fire when opened

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

If I save the file and close the file. Try re -opening file then macro does not fire.
Using Excel 2010.

Code:
[COLOR=#1f497d][SIZE=3][FONT=Calibri]Private Sub Workbook_Open()[/FONT][/SIZE][/COLOR]
[COLOR=#1f497d][SIZE=3][FONT=Calibri]    Application.WindowState = xlMaximized[/FONT][/SIZE][/COLOR]
[COLOR=#1f497d][SIZE=3][FONT=Calibri]MsgBox "This is auto open macro in This workbook"<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT][/SIZE][/COLOR]
[COLOR=#1f497d][SIZE=3][FONT=Calibri]End Sub<o:p></o:p>[/FONT][/SIZE][/COLOR]

Your help would be greatly appreciated.

Biz
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Works fine for me. Make sure you put the macro in the "ThisWorkbook" object not a standard module.

AMAS
 
Upvote 0
And works for me too..

If it is in the This workbook and not in a module try adding application.EnableEvents=True
 
Upvote 0
If it is in the This workbook and not in a module try adding application.EnableEvents=True
If the existing code is not 'firing' because Events are disabled, adding this line to the existing code won't make any difference.


Biz
If disabled events is the problem, and it could very well be, some manual action is required to re-enable them. Here is one way.

In the VBA window, ensure the Immediate Window is visible (View|Immediate Window) and then on a new line in the Immediate Window, type
Application.EnableEvents=True and press Enter

Now try saving, closing and re-opening your workbook.
 
Upvote 0
Hi,

Tried Application.EnableEvents=True and it is working now. Thank you all.

I tried code below it works but Ctrl + F4 shortcut and also clicking on red x does not work.
Code:
Private Sub Workbook_Activate()
Application.EnableEvents = True
Call umUnProtect
 ActiveWindow.WindowState = xlMaximized
Call umProtect
MsgBox "This is Workbook Activate macro in This workbook"
End Sub

Any suggestions?

Biz
 
Last edited:
Upvote 0
Hi All,

I dropped protect Windows and now everything working like a charm.
Hope this helps someone else with the similar problems.



Code:
Private Sub Workbook_Activate()
Application.EnableEvents = True
Call umUnProtect
 ActiveWindow.WindowState = xlMaximized
Call umProtect
'MsgBox "This is Workbook Activate macro in This workbook"
End Sub

Biz
 
Upvote 0
Hi Guys,

I found hack and now I can protect Windows and still ctrl +F4 or clicking red x closes the file.
The change in code in bold red.


see code below:

Code:
Private Sub Workbook_Activate()
Application.EnableEvents = True
Call umUnProtect
 ActiveWindow.WindowState = xlMaximized
[COLOR=red][B]Application.OnKey "^{f4}"[/B][/COLOR]
Call umProtect
'MsgBox "This is Workbook Activate macro in This workbook"
End Sub

I know this is not neat way but it works.

Biz
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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