Saving Personal.xls automatically - excel

I use Excel 2003 on a Windows 7 Professional setup.
In my Personal.xls file, I have compiled snippets of code/formulae that I have picked up from different places, to be available for ready reference when I use Excel. These contain a number of volatile functions such as cell(), rand(), today() etc. As a result, when I close Excel, it asks me whether I would like to save Personal.xls.
I would like to keep my Personal.xls as it is, and yet disable the popup somehow. I am fine with saving, not saving, either way, as I won't be changing Personal.xls.
I have tried the following code in my personal.xls in the Workbook_BeforeClose section
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Save
End Sub
But it doesn't seem to work. I have tried some variations, including .Save and .Saved=True, and also tried to alternatively use ThisWorkbook., Me. & Workbooks("PERSONAL.xls"). However, Excel still asks me save Personal.xls
I have also tried to disable calculations in my Personal.xls viz.
Private Sub Workbook_Open()
For Each ws In ThisWorkbook.Worksheets
ws.EnableCalculation = False
Next
End Sub
This doesn't solve the problem either. Finally I tried to do a 'ThisWorkbook.Save' after changing calculation mode to manual, but that doesn't change anything either.
Am I missing something here? Any advice would be appreciated. Thanks in advance!

You want:
Me.Save
In your workbook BeforeClose event.

Why not just force-shut the document?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close savechanges:=True
End Sub
For what I was trying to do I am actually using savechanges:=False. Both ways worked.

I just remove the Cancel As Boolean
Private Sub Workbook_BeforeClose()
ThisWorkbook.Save
End Sub

Related

Workbook_Open() does not work with Workbook Protection

I am using Excel 2016 and have this code written in the ThisWorkbook object in VBA:
Private Sub Workbook_Open()
ThisWorkbook.Protect (password = "password")
End Sub
It is not working. The point here is that this should prevent users from touching the Power Query functions in this workbook. I have saved it as a Macro-enabled workbook, I have all macros and events enabled and this is the only workbook open.
I also have this additional code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Protect (password = "password")
ThisWorkbook.Save
End Sub
And that is not working either. It works fine if I insert that "ThisWorkbook.Protect" code into a general module or worksheet object and run it manually, but when I want this particular excel file to run this code automatically on open or close, it does not do it.
Any ideas what could be causing this?
For some reason running ThisWorkbook.Protect on a protected workbook seems to unprotect it (even though I couldn't find any documentation that says that it does this) so try this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ThisWorkbook.ProtectStructure Then ThisWorkbook.Protect Password:="password"
ThisWorkbook.Save
End Sub
In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module.
If an Auto_Open, Auto_Close, or other automatically-running subroutine is stored "behind" a worksheet or ThisWorkbook, it may not function correctly when you open or close your workbook, or when you perform an action that should cause the subroutine to run.
MS Topic Discussion - Code "Behind" a Workbook

Strange behavior when closing a workbook twice

Strange issue when closing a workbook twice from VBA (Workbook_BeforeClose)
Hi. This problem appears to me in an extremely simple workbook: Workbook_BeforeClose only.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close SaveChanges:=False
End Sub
If I open and close the workbook twice, the main Excel screen looks like this, and it's impossible to do something, I can only close it from the status bar:
If all you are trying to do is to not prompt the user to save changes, just play with the appropriate flags to 'trick' Excel that changes have already been saved.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
This will allow the workbook to close, without prompting any changes to be saved, but this does not actually save them.
Notice the subtle difference between the words: Me.Saved and Me.Save.
Saved is a property that gets flipped to False when Excel detects changes were made as of the last save.
Save is a method - not a property as above - that actually will save the workbook.
Your workbook is already closing, which is what fired this event to begin with. No need to try to close it again within this event. Just tell Excel that no changes have been made since the last save, and it should close all on it's own - without the prompts.
It's possible you're re-triggering the event. Try something like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Static InProgress As Boolean
If InProgress Then Exit Sub
InProgress = True
ThisWorkbook.Close SaveChanges:=False
End Sub

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

Workbook_Open() won't execute Excel 2011

Using Excel 2011 (should be same as Excel 2010)
Code is under the "ThisWorkbook" module in Excel
Events are enabled
Macros are enabled
I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.
Private Sub Workbook_Open()
ActiveSheet.Range("BL4").Value = "Open is working"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
First make sure you have put this in this in the right place and have macros enabled.
Then, try adding this line to the workbook_open method:
MsgBox "HELLO"
Do you see the msg box? You're choice of cell looks a bit strange
Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)
FInally, if a plugin calls something like this line, it could cause your events not to fire..
Application.EnableEvents = False
So make sure you have tested it with no other sheets or addins open.

Turn Off Msg for Update Links for Excel Spreadsheet upon Opening

I have a workbook that I would like to open and not have it ask to update links, (exact message is :
"This workbook contains links to other data sources. If you update the links, Excel will attempt to retrieve the latest data. If you odon't update the links, Excel will use the previous information. Note that data links can be used to access and share confidential information without your permission and possibly perform other harmful acts. Do not update the links if you do not trust the source of this workbook." )
What I would like to do is open the workbook by clicking on the file in Internet Explorer and have the links update but not ask for the user to click the button to update.
I have tried the following code in the Open Event for the work book with not success:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
End Sub
I have also tried the following lines of code in the above Sub:
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.FullName, Type:=xlExcelLinks
Application.ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.FullName, Type:=xlExcelLinks
Application.ActiveWorkbook.UpdateLink
Workbooks.Open ActiveWorkbook, UpdateLinks:=True
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
The version of MS Excel 2010 and saving to an .xls file for sake of those with legacy versions.
Your help would be very appreciated. Thank you in advance for all your help.
Respectfully,
Robert
Just in case this might help anyone in the future the following is what I did:
Private Sub Workbook_Activate()
Application.AskToUpdateLinks = False
End Sub
This prevented the Update Links message box from appearing when the file is opened.
Robert
Just to add to Robert's (#user2320821) answer -
I had to modify the code to:
Sub Workbook_Open()
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Application.DisplayAlerts = True
End Sub
The key differences being that
1) It's a Workbook_Open sub instead of a Workbook_Activate sub. The Activate sub was not suppressing the Update Link request.
2) I had to throw in a DisplayAlerts flag toggle to suppress a second warning about the links not being updated, even after the first Update Link request was suppressed.
In case it wasn't obvious in Robert's answer, this sub worked when I put it in the ThisWorkbook object.
I'm using this code in my vba .xlm file..
Private Sub Workbook_Open()
ThisWorkbook.UpdateLinks = xlUpdateLinksNever
End Sub
M Office2013

Resources