Macro with Application.OnTime Only Works When worksheet is Opened - excel

I have a Worksheet that requires a macro to run every day at 10AM. The code only works when I have a specific worksheet opened, but doesn’t when another worksheet is open. Can somebody please help me?
Code in Module:
Sub SendC()
Application.OnTime TimeValue("10:00:00"), "SendC"
**Rest of my code **
End Sub
Code in ThisWorkbook:
Private Sub Workbook_Open()
Application.OnTime TimeValue("10:00:00"), "SendC"
End Sub

VBA code lives embedded in a host document - in the case of Excel, a macro-enabled Workbook file.
By closing the workbook that contains the code, you terminate whatever VBA code was running in that workbook.
You can make VBA code run at application-level, by making an Excel add-in: the code still lives embedded in a host document, but that host document is loaded along with Excel, and remains loaded as long as that Excel instance is running.
Save your VBA project as an Excel add-in, then close everything, bring up Excel again and load the add-in: Workbook_Open will run once as the add-in is loaded, and then your SendC macro will be invoked at 10AM for as long as you leave Excel running.

Related

Trigger a VBA Macro on any opened workbook and sheets with following code

I am using the following code to highlight spelling mistakes in the cell-on-cell change
Sub ColorMispelledCells()
For Each cl In ActiveSheet.UsedRange
If Not Application.CheckSpelling(Word:=cl.Text) Then _
cl.Interior.ColorIndex = 15
Next cl
End Sub
Now, the problem is that every time I have to run this code by pressing f5 and also this only works on a particular workbook (Workbook specific).
So, my question is, what is the process to make a universal/global macro that runs on every workbook and every sheet which is opened, and that too runs internally when the cell is changed? It should not open up with the VBA editor window again and again.
Along with this is there any way where I can make a enable and disable button highlighted on the excel toolbar for this macro?
see image to see my VBA editor project explorer hierarchy
I tried searching on the internet and also saw many sample codes but nob body explained to make a global/universal macro, I am unable to figure out how to develop a global/universal macro that runs automatically on cell changes. which is not bounded to any one workbook but works globally on any opened workbook on all sheets.
I'm interested in your case. I myself is not an expert, so just now I search the internet and playing around and try to "cheat" by making this kind of code - which I'm not so sure if that meets your requirement.
In PERSONAL.XLSB workbook - ThisWorkbook module :
Private WithEvents app As Application
Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
On Error Resume Next
If Wb.Name <> "PERSONAL.XLSB" Then
Set src = Workbooks("PERSONAL.XLSB").VBProject.VBComponents("Sheet1").CodeModule
Set trg = Wb.VBProject.VBComponents("ThisWorkbook").CodeModule
trg.insertlines 1, src.Lines(1, src.countoflines)
End If
End Sub
Private Sub Workbook_Open()
Set app = Application
End Sub
In PERSONAL.XLSB workbook - Sheet1 module :
Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range)
If Not Application.CheckSpelling(Word:=Target.Text) Then _
Target.Interior.ColorIndex = 15 Else Target.Interior.ColorIndex = xlNone
End Sub
Source code from this link and this link
The sub in "ThisWorkbook" module of PERSONAL.XLSB will be triggered when opening any workbook. The sub will copy the macro in "Sheet1" module of PERSONAL.XLSB into "ThisWorkbook" module of the open workbook.
The sub in "Sheet1" module of PERSONAL.XLSB is an event handler to any sheet of the active workbook which will be triggered if there is a change on any cell of any sheet.
I noticed that if I edit the sub in the PERSONAL.XLSB to something else, then I open another workbook, the sub will not run. I need to close the Excel application first, then open it again. If I don't do any sub-editing, opening other workbook will trigger the sub to run.
The animation above is opening two xlsx workbook which for sure there can't be any code in that workbook. But because at the time it's opened the macro in PERSONAL.xlsb is triggered (copying a sub to "ThisWorkbook" module of that xlsx workbook), so then there is a sub in that xlsx workbook. If I close this xlsx workbook, it will prompt me if I want to save the workbook. If I click yes, it will complain because it can't be saved as xlsx while there is a macro in "ThisWorkbook" module of this workbook.
Still not sure though if this is the kind that you want. Also maybe what you want is something like this : in whatever computer which has Excel app, it can do the same thing without doing anything before hand. So it's not doable, because to any computer which has Excel app, the macro need to be copied to PERSONAL.XLSB on each computer before hand.
Please note, I don't test to open an xlsm workbook which already has Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module. And I think most likely it will throw an error if the opened workbook has already Private Sub Workbook_SheetChange(ByVal sh As Object, ByVal Target As Range) in it's "ThisWorkbook" module

Open Excel worksheet - run time error with other macros

I made a workbook with several sheets for data entry, and I want the index/main sheet to display upon opening every time.
Everything on the internets says to use something like below, but I get a runtime error.
I've made sure the worksheet is named "Attendance Main" and the spelling is correct.
Most troubleshooting issues I've seen are about activating sheets for copying/pasting and other actions. I just want the main sheet to display each time the workbook is opened.
When I create a new workbook, with 2 basic sheets, the macro works. So I am wondering if other macros are interfering with it. However, none of my macros are in "ThisWorkbook", most are in their own modules.
Any help appreciated, and if you need any info from me to narrow down the issue I will try to provide what I can.
Private Sub Workbook_Open()
Worksheets("Attendance Main").Activate
End Sub
Run-time error '1004'
Activate method of Worksheet class failed
For me a simple select code inside the open event of Workbook works fine.
Private Sub Workbook_Open()
Sheets("ShowThisFirst").Select
End Sub

Is it possible to prevent code available in personal.xlsb?

I've been self-teaching myself VBA for a couple of years now (working on it on and off to make my work life easier). I'm usually able to find answers to my questions on this forum, but not this one.
I have a lot of 'example' code in my own personal.xlsb. For instance I have a public sub with a modified messagebox with standard caption etc I always like to make visible, referred to as PublicSub_MyMsgBox
Often, the automation I create, will be used by my colleagues as well. I would create a specific workbook with specific buttons for a specific goal. I would copy the required code into a module in this shared workbook. The workbook would therefore have a module with the sub PublicSub_MyMsgBox as well.
Another sub in this shared workbook will have a line to call PublicSub_MyMsgBox.
How can I be sure that when I test a code created in this shared workbook, it does not use the PublicSub_MyMsgBox from my own personal.xlsb, but actually only uses the code from the workbook?
(to be able to verify that I have indeed copied all relevant code into the workbook for my colleagues to use as well).
Thank you very much for your help!
Linda
No code from personal.xlsb will run even if a reference to pers. workbook exists! You can be sure that no code from personal.xlsb will be run, instead of the one inside the workbook where the call is made.
Even if you added a reference to your personal.xlsb, for identic macro names VBA will choose the one in the workbook where the code runs. To check that, please, try the next scenario:
Create in your personal workbook the next macro:
Public Sub MyMsgBox()
MsgBox "Hello from Personal workbook!"
End Sub
Then create the next macro in a standard module of your new workbook:
Sub MyMsgBox()
MsgBox "Hello from This workbook!"
End Sub
Create a simple checking Sub, of course, in the new workbook, add a reference to your personal.xlsb workbook and run it:
Sub testMyMsgBox()
MyMsgBox
End Sub
It will run the Sub in your workbook. Now, comment the Sub from your workbook and run testMyMsgBox again. It will ran the procedure from personal.xlsb...

How to fix disabled excel macro file, when workbook is protected with vba?

When I open macro enabled excel file, the macro becomes disabled in protected workbook (VBA code does not run). But when protection is removed from the workbook, the macro file becomes enabled and the VBA code runs fine.
Is there is any way to protect the workbook and also to run VBA code in macro file simultaneously.
Sub test()
ActiveWorkbook.Unprotect ("mypassward")
' my vba code
ActiveWorkbook.Protect ("mypassward")
End Sub

Workbook_Open() in Excel 2016 not firing

Excel 2016 (or 365) does not seem to fire the Workbook_Open() sub reliably or more precisely, not at all!
The simple event sub
Private Sub Workbook_Open()
MsgBox "Work book is open"
End Sub
does not seem to work. However, if a workbook is already open and then the workbook containing the above Sub is then opened, it does run as expected.
I notice that unlike Excel 2010, 2016 (365) opens each workbook in its own window, not a workbook window in the Excel application window. Is this a bug in 2016 and is there a workaround?
I have produced a work around for my own applications and that is call the activation of a worksheet and call my initialization routines from there. But a bit "rough" and it would be good to have the Workbook_Open() sub working correctly.
It is a simple single Sub in the ThisWorkbook module. Macros are enabled. In Excel 2010 it works perfectly, as do two other macros in other workbooks for which I have written macros. It is just this one thing with Excel 2016. Given that the Workbook_Open() sub is the gateway to a workbook it seems a little strange that we have to go to a workaround to make it function.
Try encapsulating the call with a new instance of Excel. Example below:
Sub OpenInNewExcel()
Dim Background_Excel As Excel.Application
Dim pathName As String
Dim fileName As String
Let pathName = "Enter your path here" 'include "\" at the end
Let fileName = "Enter your file name here"
Background_Excel.Workbooks.Open fileName:=pathName & fileName
Background_Excel.Parent.Quit ' This is how you close the file completely using VBA otherwise the file will close and the Excel Shell will remain.
End Sub
Also make sure that enable macros is turned on in the Options-Trust Center.
You have to add the file/folder location of your workbook as a "Trusted Location".
You can see more info about that in Designate trusted locations for files in Office 2016.
I have same problem then I found solution after google it:
https://www.myonlinetraininghub.com/excel-forum/vba-macros/excel-2016-workbook_open-event-doesnt-trigger
Then I also use "Private Sub Workbook_Open()" and "Public Sub Auto_Open()" open in excel 2016 that work fine:
Private Sub Workbook_Open()
CustomStartUp
End Sub
Public Sub Auto_Open()
CustomStartUp
End Sub
Private Sub CustomStartUp()
MsgBox "Work book is open"
End Sub
I've had this problem (I'm using Microsoft 365), and I found this thread.
It happens for me sometimes when I have another workbook already open, then, on trying to open my macro-enabled workbook, before any sheet is displayed I get the Microsoft warning message about macros. Then, although I click its 'Enable' button, the Workbook opens, macros do get enabled, but Workbook_Open doesn't run.
I've never known the problem to occur if no other workbook is open. (Of course, the user might still get the yellow-backed messages at the top of the workbook, asking them to click the Enable Editing and/or Enable Macros buttons.)
Note that my Workbook_Open just calls another 'workbook-opening' sub in a module to do all the opening processing.
My solution: When my workbook-opening sub is called, it sets a global variable to True to indicate it has run.
I've made it obvious to the user that the problem has occurred, by means of a 'Welcome' sheet with all its cells locked, so the user can do nothing; at this point all other sheets are very hidden. The workbook-opening sub, when it runs, deactivates this sheet and makes it very hidden, so the user never normally sees it, and makes the other sheets visible. But if this screen remains, it instructs the user to select the other workbook, then select this one again. My Workbook_Activate code then runs, and because the global variable isn't True, it calls the workbook-opening sub. If this global variable is True, it does nothing.
To make this work, the Workbook_Close sub makes the other sheets very hidden and the Welcome sheet visible, ready for the next time the Workbook is opened.
Hey presto, the problem is solved.
The Welcome sheet actually has a dual purpose, in that if either of the yellow-backed warning messages are displayed, it will remain and force the user, with suitable instructions, to click Enable Editing and/or Enable macros. If the users aren't au fait with macro-enabled Excel, they will just ignore these and try to carry on regardless.
All this is much easier to implement than to explain. I hope it's clear.
And I hope this might be of help to someone.
I had this issue with one of my files as well. I managed to fix this issue by running Workbook_Open manually in the VBA editor once open and saving the file in another location. The file in the new location should have no issue with auto-running Workbook_Open. If this doesn't work, copy the original file to a new location before manually running & saving.
If the newly saved file does not run Workbook_Open, repair your version of Office.

Resources