I have a code that I am running that is stored in ThisWorkbook. It was originally Private Sub Workbook_Activate and Private Sub Workbook_Deactivate. This was causing some unexpected results when switching between open workbooks. So I have changed the code to what is below. Only now, I get a compile error message (the Private Sub Workbook_BeforeClose is highlighted when I get this error message) that says:
Compile error:
Procedure declaration does not match description of event or procedure having the same name.
I am not savvy enough in VBA to understand this error message or a good fix for it. All help and suggestions are welcome.
Private Sub Workbook_Open()
Call AddToCellMenu
End Sub
Private Sub Workbook_BeforeClose()
Call DeleteFromCellMenu
Call DeletePopUpMenu
End Sub
It should be:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
The Cancel parameter is there to let you prevent the workbook from closing - e.g. if certain conditions haven't been met. Setting Cancel to True stops the workbook from closing - see the Workbook.BeforeClose documentation
Related
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 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
I get the ambiguous name prompt with this excel vba:
Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As Boolean)
ActiveWorkbook.CustomDocumentProperties(“DepartmentCode”).value = Range(“DepartmentCode”)
ActiveWorkbook.ContentTypeProperties(“DepartmentCode”).value = Range(“DepartmentCode”)
End Sub
There is another Public Sub Workbook_BeforeSave event in the same workbook which is also required. How do I work around this.
I have no knowledge of VBA. I found the string above on google. I am basically trying to synchronize data in excel with columns in SharePoint.
Would appreciate help on this.
How can I work around it? Sorry, I have no knowledge of vba. Just copied from google
Copy all the code in one event (that is, everything after the Private Sub line and before the End Sub line) and paste it into the other event, either right after the Private Sub or right before the End Sub. Then remove the whole event you copied from.
I'd like to save my spreadsheet every time I lock my windows pc. Is there a built-in trigger in excel (similar to on-close)? Or might I be able to get a macro to run every minute and check if my machine is in use or locked?
If you really need this you can try the following workaround:
Enable auditing for the lock event in the event viewer, so that event 4800 for locking is logged, as described here.
Create a scheduled task triggered on the lock event. Choose Trigger on Event with Protocol: "Security", Source: "Microsoft-Windows-Security-Auditing" and Event-ID: "4800"
Run a vbscript that uses GetObject(, "Excel.Application") to retrieve the currently active excel session and run a macro that saves for you with Application.Run (like in this example)
Not very straight forward, but on the other hand, the easy way would just be to press ctrl+s everytime before you lock your computer.
You could try this and see if this works instead of using a macro:
Click the Microsoft Office Button , and then click Excel Options.
Click Save.
Select the Save AutoRecover information every x minutes check box.
In the minutes list, specify how often you want the program to save your data and the program state.
But if you specifically want a macro I could try to form one for you.
Okay well add this into a standard module:
Option Explicit
Public RunTime
Sub StartTimer()
RunTime = Now + #12:10:00 AM#
Application.OnTime RunTime, "SaveBook", schedule:=True
End Sub
Sub SaveBook()
ActiveWorkbook.Save
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime RunTime, "SaveBook", schedule:=False
End Sub
Place this in your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Then add this into your workbook class module:
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
StopTimer
End Sub
Have fun with it and fiddle around with it
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.