run workbook open event from workbook saved with new name - excel

I have code which uses the saveas dialog to create and open a new workbook based on certain criteria which works perfectly however the newly created workbook does not execute the workbook.open event which opens a userform.

Opening a workbook in code will not cause the auto-open event to run. You need to add the line:
wb.RunAutoMacros xlAutoOpen
... to your code, assuming wb is the workbook you just opened.

Related

Exit application without closing all the files

I am trying to provide an option to my users to save and close excel application after they finish to complete a specific form. However, it is closing the whole Excel and not only the current Excel workbook that they just used. In other words, if they have 4 different Excel files opened, it will close all of them and not only the current workbook. What am I doing wrong?
This is the code that I am using:
ThisWorkbook.Application.Quit
ThisWorkbook.Save
You must not quit the Application - but close the workbook:
ThisWorkbook.Close SaveChanges:=True
will save and close the workbook.
If it is the only open workbook, Excel gets closed as well.
If not Excel and the other workbooks are still open.

Open Second Workbook Triggers VBA Code in First Workbook

I open an Excel workbook which has some VBA code in it including Event code on one of the worksheets. I open a second workbook which has no VBA code in it at all but as soon as the second workbook opens, it triggers the VBA code to run in the first workbook. The code fails because the first workbook is not the Active Workbook. Anybody got any ideas why opening a second workbook should trigger event code in the first workbook?
I can replicate that -seems like opening a workbook triggers the calculate event in other open workbooks. Having said that, your event-handling code should not rely on any particular workbook being active when it runs, so it would help to post that if you want suggestions for fixes.
For example the worksheet object which corresponds to the sheet module can be referenced via Me, and the workbook containing that sheet is Me.Parent
Private Sub Worksheet_Calculate()
Debug.print "calculating " & Me.Name & " in " Me.Parent.Name
End Sub

Hiding a workbook while being able to close others

I got a workbook that automatically opens a UserForm.
The workbook itself I hide using Application.Visible = False
While in the UserForm the user is able to create a new workbook (.CSV file) and this all works.
But if the user tries to close the newly created workbook it will ask if you want to save it AND also want to save the original workbook (closing it in the process). If I select No on the first menu (new workbook) and cancel on the second (the original workbook) both workbooks remain open. If I don't select cancel on the second menu both will close.
Is there a way to hide my workbook and later on closing a different workbook without being forced to close the hidden workbook?
Update:
A current workaround I have is putting a button on the UserForm to close any newly created workbook with Application.DisplayAlerts = False enabled during this process (and of course setting it to true after) .

How to Activate a specific Workbook on non-VBA Excel Quit

I have a workbook ("CodeBook.xlsm") that runs code using a BeforeSave event. When a user has multiple workbooks open and chooses to quit excel via File/Exit, the user is prompted whether to save workbooks, and if yes to CodeBook.xlsm, then the BeforeSave code is run. The problem is, at that point the ActiveWorkbook may not be CodeBook.xlsm, unless that happens to be the workbook that the user was in when he/she selected Exit Excel. If the user quit excel from another workbook, the BeforeSave code is running but the activeworkbook is some random file of the user, so all the references to specific worksheets and ranges in the BeforeSave code do not work.
I have tried various ways using a Static declaration to retain the name of CodeBook and workbook().activate to activate it when the application is quitting, but when BeforeSave runs, it can't pick up the name CodeBook anywhere, short of hard-coding the name into the code.
Any suggestions? How to retain a variable name in memory when there is no code running, but is there when a user initiates a quit excel, OR how to activate a specific workbook when Excel is quitting from a user command and not from application.quit. Using excel 2010.
I overcame this by including a reference to the specific workbook.
For example, this code simply saves the date/time stamp in cell A1 of Sheet1 before closing the document. By adding ThisWorkbook, it works on the specific workbook that the code resides in. If you don't add ThisWorkbook then it will work on the active workbook when the user quits.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Now()
End Sub
Place this code in the ThisWorkbook module.

Find if workbook was opened using VBA or by user

I have a macro workbook Excel 2007 with a workbook open event.
Inside the workbook open event is it possible for me find if the workbook was opened by the user from windows or from another workbook using vba...
I am not sure if it fits your needs, but you can open a workbook in your VBA code without firing the events:
'Disable Events.
Application.EnableEvents = False
'Open your book.
Workbooks.open(Filename)
'Enable Events.
Application.EnableEvents = True
If this suits your purpose:
When opening with vba, change the name of the workbook before opening. Then change it back when done. Workbook_Open procedure just needs to check for the name of the workbook it is in.
To change the name of the workbook
you can open it, then use SaveAs method to close it again with the new name
use the Shell function
use a Microsoft Scripting Runtime object.

Resources