I've got a Workbook_Open event macro (and it's in ThisWorkbook) that doesn't always run.
If Excel is closed and I double-click the .xls file from Windows Explorer, it does run.
If I launch Excel from Start/Programs (with no .xls file) and then open the file, it does run.
If I've already opened the file in Excel, but then close the file (leaving Excel open) and reopen it, then the macro does not run.
I've got security set to medium and I enable macros whenever it opens.
What do I need to do to get the macro to run whenever I open it, not just the first time for this Excel session?
(btw, this is Excel 2003)
I thought that this was the most cogent article on this problem (which is a long-standing never explained completely erratic bug that Excel exhibits). (dead link)
In short, in many cases it's a timing thing caused because the workbook is trying to calculate stuff when it opens and that gets in the way of the Workbook_Open event. The way to test on yours to see if that it for this situation, is to first rename any UDFs or Macros called by cells so that they won't get called and see if that changes anything.
I experienced the same problem.
I tested a workbook on my computer without any troubles. After destributing it to my customers I was told, that some combo-boxes stayed empty. These are usually filled from inside the workbook_open routine.
I tried different things to enable the workbook_open-Event - without success.
Finally, I found that disabling all userdefined Functions (UDF) lead to correct execution of workbook_open.
As my workbook is opened from another file, I will try to set calculation to manual first and then run the workbook_open manually. This may be done by defining it
public sub workbook_open
instead of
private sub workbook_open
Strange, that excel does not time this by itself...
A late answer (better than none).
I've had this problem now a few times (with Excel 2010).
The solution that has always worked (so far) was: remove conditional formatting, in particular if it contains UDF as conditions. As #LanceRoberts wrote in an above post, it's ultimately due to UDF calculations "overriding" the Open event, but I've found that those are particularly harmful if used in conditional formats.
I was experiencing almost identical behavior, and found that it is due to a bug that occurs if conditional formatting rules are erroring out. It turns out that if the conditional formatting rules are based on any setup by the macros, and that causes the conditional formatting to error, the Workbook_Open macro will not even attempt to run.
To test, make a copy of your file, and delete all conditional formatting from the workbook. Save and reopen. If it fixes your issue, then rework the conditional formatting rules to not depend on functions/values that will be broken before the Workbook_Open macro runs.
A few suggestions:
Try signing the workbook with a digital certificate. Add this certificate to the Trusted Certificates store then try again.
If this is machine-specific, try re-installing Office.
Make sure you have the latest service pack(s) applied.
I encountered the same problem, and I avoid it using the security settings.
I use the options settings then confidentiality center, then "params of confidentiality center" (sorry but its a translation of the french version :-p)
then "files approuved" or something like this.
And add the file containing the excel workbook in.
And its finnaly worked after that.
Looked everywhere and never find that solution.
Hope it'll help someone
This happens when a workbook is closed with an Application.EnableEvents set to false, and then you open another workbook within the same instance of excel opened. To avoid this, make sure that all of your processes that disable events, reenable them before terminating. Special attention to "End" commands, error handlers and "exit sub" sentences in the middle of your program.
What causes it is that your other archive, the one you openned first, have a Workbook_Open procedure; Excel doesn't excute it a second time.
This happened to me also and took me hours to figure out.
Turns out the TODAY() function in Excel was causing the problem.
Once deleted from my worksheet everything worked again. Very strange bug.
To add to the Arturo Llano post: The following code was used to monitor the Workbook_Open event and then run ProcessX whenever a workbook was opened.
ProcessX contained an End statement. The result was that it worked only the first time. The End wiped out AppX, so there was no further monitoring of events. Removing End fixed the problem. (Using End is bad practice anyway as it stops everything without any kind of cleanup or termination of other resources).
'Code in: Personal.xlsb ThisWorkbook
Public WithEvents AppX As Application
Private Sub Workbook_Open()
Set AppX = Application
End Sub
Private Sub AppX_WorkbookOpen(ByVal wb As Workbook)
'A 1-second delay to allow opening to complete before ProcessX starts.
Application.OnTime Now + TimeValue("00:00:01"), "ProcessX"
End Sub
Related
The following paragraph added to explain the issue without the details.
A shortcut key (Ctrl+Shift+Z) exists in two workbooks and both work as expected when opened separately. However, if both workbooks are open the shortcut always fails and always in the same workbook. The order the workbooks are opened does not matter.
The following command has been executed in two different workbooks. The Public Subroutines executed have different names and only exist in their respective workbooks.
Application.MacroOptions Macro:="SelectAndReveal", ShortcutKey:="Z" 'Ctrl+Shift+Z
Opening order doesn't matter; wkbkA then wkbkB or wkbkB then wkbkA. The shortcut only works in wkbkA. If wkbkA is closed, it immediately begins working as expected in wkbkB.
This code was even added to "ThisWorkbook" of wkbkB, compiled, and saved. It still doesn't work in wkbkB if WkbkA is open.
Private Sub Workbook_open()
Application.MacroOptions Macro:="SelectAndReveal", ShortcutKey:="" 'Clear Shortcut
Application.MacroOptions Macro:="SelectAndReveal", ShortcutKey:="Z" 'Ctrl+Shift+Z
End Sub
Must the shortcut be set and cleared in Workbook_Activate and Workbook_Deactivate events to ensure the shortcut works? Why doesn't it work in the active workbook, or at least the last workbook opened?
PS. I want to use Ctrl+Shift+Z because, as I recall, there were only two Ctrl or Ctrl+Shift keystrokes that don't compete with Excel or the system. Plus, the other keystroke requires two hands.
FYI: It only executes in wkbkA when wkbkA is active.
Final footnote: Until this issue was encountered today the MacroOptions command had been set once. It was not executed by code since the first execution.
The workaround is to clear the shortcut when the workbook loses focus (no longer active) and reset it when it regains focus.
Ok so it is a very minor bug. How the bug manifests itself is very strange. When both workbooks are opened the shortcut always works in one workbook but never the other regardless of the order the workbooks are opened.
There are tons of questions already posted with a similar problem, but none of those solutions works for me. I'm using Office 365, so maybe it's something that changed there. Anyway, here is my issue.
I have a file that pulls info from one main tab into other tabs. On the subsequent tabs, I have VBA that auto-hides rows depending on the value that was pulled from the main tab. Anytime the data in the main tab is updated, the subsequent tabs should also update. I had an issue with making this work, but I worked it out by using another cell to store the previous value that had transferred (comparing them, and running the code any time they were dissimilar). The problem now is, of course, that it wont run when the tabs are protected. The tabs need to be protected to prevent end-users from corrupting the formulas and formatting, but I can't make it work with any of the solutions posted on StackOverflow. My code looks something like this:
Sub Worksheet_Calculate()
If Range("Old Value") <> Range("New Value").Value Then
Range("Old") = Range("New").Value
Rows.EntireRow.Hidden = False
<<Insert code to auto-hide based on "New Value" cell>>
End If
End Sub
I've tried adding Worksheet.Unprotect Password:= and Worksheet.Protect Password:= commands on either side of the If statement, and I get a 424 Object Required error. I've tried using the "UserInterfaceOnly = True" command with my password protection, and it changes nothing (still get the trying to change a protected sheet error). I've tried creating another subroutine for unprotecting and re-protecting, then using a GoTo to run the original subroutine; but that didn't work either. I see a lot of solutions using ActiveSheet commands, but I don't think I can use them. I'm updating the main tab, and the action is happening on another tab. I'm fairly new to coding, and I'm teaching myself as I go; so, I could be missing something obvious.
It works perfectly when unprotected, so I guess I may have to just leave it that way. I hate wasting time fixing the files when end-users do their best to ruin them, though, so I'd love to leave it protected.
This may not be the final answer, but I seem to have stumbled upon a solution. Since the only thing I want happening on the protected sheet is for rows to auto-hide, I left row formatting unprotected. It seems to work fine that way. You would think there's a way to run your VBA normally without having to jump through hoops because of protection....but, I'll take it for now.
I am building a database in Access, for which I import data from an Excel workbook questionnaire. I have coded an Import-sub that selects, opens, retrieves the data from and finally closes the workbook.
The problem is this: for some reason, when I now open any excel workbook on my computer (at a time when neither Access or Excel is in use) some old version of the questionnaire keeps opening as well. This problem doesn't end by restarting the computer, but only by deleting that specific questionnaire-workbook. But then it starts happening with another workbook.
I have a theory that this might be because I - in my import-sub - have opened the questionnaire, encountered a run-time error which has ended the sub before it closed the workbook, and that somehow the workbook is still "open". Or that there still is a link active.
So I have two questions:
1.) Does anyone know how I can fix this problem?
2.) Is there generally any consequences of not closing a workbook that you open through VBA?
My relevant code is:
Dim MyXL As Excel.Application
Dim MyWB As Excel.Workbook
...
in between lots of stuff happening, several times an error occurs which interrupts the program.
...
MyWB.Close False
MyXL.Quit
Appreciate any help on this!
I did Encounter the same Problem and found out that Excel stores the files that open whenever you start Excel in a Folder (XLSTART). The path to mine was: c:\USERS\MyUserName\AppData\Roaming\Microsoft\Excel\XLSTART
As suggested by Ross McConeghy error handling may prevent such an occurrence. But the error already happened and the questionnaire, as you suggested, has placed that workbook in the Folder XLSTART. You have to delete it from that folder to fix the unwanted occurrence.
Your theory is likely. If you never display the Excel application, errors are encountered, and your code never closes the workbook it would be left open in the background and your code would most likely lose reference to it. The next time you open an Excel file the instance of Excel that is already open(but hidden from view) "picks up" the request and opens the file, also displaying the file that was still open from the macro.
1.) You should set up error handling so that the Workbook and Excel application are closed when there is a non-recoverable error.
Sub MySub()
On Error GoTo handle_Err
<... code ...>
Exit Sub
handle_Err:
MyWB.Close False
MyXL.Quit
End Sub
If you have other Error handling statements in your code always reset to On Error GoTo handle_Err instead of GoTo 0 etc.
2.) The only consequences of not closing the Workbook are the obvious ones- the system resources it's using and, if it is open for editing, no one else can edit it.
I had a similar problem and solved it a different way.
I found the connection to an external workbook and fixed it by going to Data > Edit Links.
I want to create an excel template that will includes formulas for dating the columns. However, since those formulas will be based on TODAY(), I need to convert them to static strings (so the dates don't change everytime someone opens it). Is there a way to add a macro that will run automatically when someone creates a new spreadsheet based on the template? (Similarly to Auto_Open(), only on Create, rather than Open). If so, I could just create a macro that will replace the formulas with their results upon document creation. Can this be done?
[Note: I'm not married to this solution; it just seemed like the simplest way to protect my spreadsheet. If someone can suggest an alternative approach, I'd be obliged.]
I have a couple thoughts...
If you run a copy/paste values macro every time it really won't matter, right?
You could check if the file exists yet (has been saved), and if not
then this must be the template opened as a new workbook, maybe?
Code:
Private Sub Workbook_Open()
If Dir(ActiveWorkbook.Name) = "" Then
'run the macro?
MsgBox "I'm gonna run this macro"
End If
End Sub
You could have a cell on one of the sheets, that will never be used,
or is hidden, that will store whether or not to run the macro, and
change that when the file is opened or when the macro is ran. Then
have a macro run on open that checks that cell. (Or custom/document property)
You could populate the cells that have the today() formula only on
open and if they are already populated then don't run the macro?
I realized that there is no need for a Workbook_Create() function, as that behavior can be emulated by simply deleting the macro after it has run once (which happens when it is first created). Deleting macros is done automatically when the file is saved with a .xlsx extension. In addition, you need to prevent the macro from running when you open the template itself (while editing the it). You can hold the SHIFT key when opening it to prevent auto-run macros, but since that method isn't foolproof, I added this code at the top:
Private Sub Workbook_Open()
'If we're opening the template, don't run the macro
If Application.ActiveWorkbook.FileFormat = xlOpenXMLTemplateMacroEnabled Then
Exit Sub
End If
...
'add code here to SaveAs .xlsx, thus removing the macros, so it won't run every time.
End Sub
(Note: I didn't show my SaveAs code as it is rather messy: I wanted to suppress the default warning about losing macros, but wanted to also protect the user from inadvertantly overwriting previous file. If anyone is interested, I could post it)
I've got a VBA macro in an Excel 2003 spreadsheet and I'd like to 'install' it on a machine running Excel.
I created a 'Trusted Location' on the local machine, and I know how to copy the module to the existing workbook and assign a key combination to invoke it, but I don't know how to make the macro appear automatically when someone starts Excel. Help?
The simplest solution is ( both for xl2003 and xl2007 ) to copy your xls containing the macro to the path = Application.StartupPath from your client machine ( can build a simple vbscript installer which instantiates excel and retrieves this information ).
This way your macro will be available in any workbook opened since the xls files located in startuppath are loaded at excel startup.
I remember the old way was to save your macro in your Personal.xls
workbook - then it would be accessible
any time you opened Excel - has this changed for 2007?
That was a way. A better way was (and is) to create an Add-in, which you can then enable & disable via Tools->Add-ins. (An Add-in will remain enabled even if you close and re-start Excel). You can save any .XLS file as an Add-in (.XLA).
Within your Add-in you could just use an Auto_Open method:
Private Sub Auto_Open()
DoStuff
End Sub
...or, you could hook up the Workbook_Open event, as Ryan suggests.
Of course, since the user can disable the Add-in, you also want to do the reverse in Auto_Close (or in the Workbook_BeforeClose event).
Actually, if you use events it'd be better to use the Workbook_AddinInstall and Workbook_AddinUninstall events. Using those is slightly more "correct", and also has the benefit that the 'close' event doesn't fire if you close Excel and then hit Cancel when prompted to save.
I'm not sure what you mean by 'appear', but you can execute it automatically by calling it from
Private Sub Workbook_Open()
End Sub
in the ThisWorkbook object module. Is that what you're looking for?
I remember the old way was to save your macro in your Personal.xls workbook - then it would be accessible any time you opened Excel - has this changed for 2007?