Installing a VBA macro in Excel 2007 - excel

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?

Related

Running Excel Add-In Macro Based on Workbook Events

I want to create a macro that takes the user to the last previously active sheet. I need this macro to keep track of all previously active sheets, which it should be able to do automatically. I want to add this to an Excel add-in, since it needs to be compatible with all the worksheets that my company's using. The sheets themselves are not macro-enabled, so I can't have any code stored in the modules of ThisWorkbook.
I have tried out the examples that I've found on other websites. Unfortunately, all of them require using ThisWorkbook, which I won't be able to use. Is there a way to do this purely within an Excel add-in?
Public MyPrevSheet as String
Sub GoToPreviousSheet()
If Len(MyPrevSheet) > 0 Then
Sheets(MyPrevSheet).Activate
ActiveWorkbook.ActiveSheet.Previous
Else
MsgBox "You have not switched sheets yet since opening the file!"
End If
End Sub
Ideally, I would like for MyPrevSheet to automatically track the last active sheet in the file.
Update: I managed to make it work using Application Events. The idea is to create a class module that can then be assigned the Application.
Here are some useful links that helped me: http://www.cpearson.com/excel/AppEvent.aspx
https://www.jkp-ads.com/Articles/buildexceladdin05.asp

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.

exec a xlam everytime I open an excel file

I have the following peace of code to exec a xlam file when I open the excel file:
Sub Auto_Open()
Application.OnTime Now + TimeValue("00:00:05"), "readCsv"
End Sub
Sub readCsv()
....
End Sub
I add it as Add-ins so every file I open has the xlam file on it. If there is no other excel file opened, it works perfectly but after that without closing the excel file I open a second one it does nothing. I need to exec the code even if there is another excel file opened. Is that possible?
I also try to do it writing the code in ThisWorkbook but the result is the same, If there is another excel file opened it does nothing.
To use your macro in any of the worksheet opened (irrespective of name) you can only possible do it by using a personal workbook Here is the Link
If the above doesnt suits you can prepare Add-ins and install it for users (Google for it if this is the case)
EDIT:
How to get add in in all opened files....
Goto File - Option - Quick Access Toolboar - From the dropdown "Choose Command from" - select Macros then select add-in macro - Add it, Below there would be a modify button Select the icon you like from it - OK
Now you will be able to see the icon with the addin function linked on top of the excel - click it for functioning, it will remain there forever ( if missed you can reapply the settings)....
The procedure are for 2010, for 2007 it should be similar....for 2003 there is a different way to achieve it...
Copy the .xlam to C:\Users[user]\AppData\Roaming\Microsoft\Excel\XLSTART. It will load every time you open Excel.

Using Excel 2010 XLAM files

I created an XLAM file which displays a customized ribbon tab, the buttons of which call various macros.
How do I get this add-in file to load automatically when opening another xlsm file?
Currently, the only way to display the add-in ribbon is to open the XLAM file first, then open the other XLSM file. Only then will the custom tab appear.
I appreciate your help.
Many thanks,
KS
In the ThisWorkbook module of the workbook (not the addin) enter something like this:
Private Sub Workbook_Activate()
Application.AddIns("MyAddin").Installed = True
End Sub
Private Sub Workbook_Deactivate()
Application.AddIns("MyAddin").Installed = False
End Sub
The word "Installed" is a bit misleading, as it only indicates whether the addin is checked or unchecked in the Addins Menu.
If by chance the ribbon is only for one workbook you should just attach the ribbon to that workbook.
If the ribbon is for multiple workbooks, people generally take the opposite approach to what you are doing, i.e., create an addin that uses application-level events to turn menus on or off when specific workbooks (or workbooks with a specific characteristics) are activated or deactivated.
If you add the file to C:\Users[user]\AppData\Roaming\Microsoft\Excel\XLSTART it will add the ribbon every time you open Excel.

Insert current date in Excel template at creation

I'm building an excel template (*.xlt) for a user here, and one of the things I want to do is have it insert the current date when a new document is created (ie, when they double-click the file in windows explorer). How do I do this?
Update: I should have added that I would prefer not to use any vba (macro). If that's the only option, then so be it, but I'd really like to avoid forcing my user to remember to click some 'allow macro content' button.
You could use the worksheet function =TODAY(), but obviously this would be updated to the current date whenever the workbook is recalculated.
The only other method I can think of is, as 1729 said, to code the Workbook_Open event:
Private Sub Workbook_Open()
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = Date
End Sub
You can reduce the problem of needing the user to accept macros each time by digitaly signing the template (in VBA IDE Tools | Digital Signature...) and select a digital certificate, however, you will need to get a certificate from a commercial certification authority (see http://msdn.microsoft.com/en-us/library/ms995347.aspx). The user will need to select to always trust this certificate the first time they run the template, but thereafter, they will not be prompted again.
You can edit the default template for excel -
There is a file called Book.xlt in the XLSTART directory, normally located at C:\Program Files\Microsoft Office\Office\XLStart\
You should be able to add a macro called Workbook_Open
Private Sub Workbook_Open()
If ActiveWorkBook.Sheets(1).Range("A1") = "" Then
ActiveWorkBook.Sheets(1).Range("A1") = Now
End If
End Sub
My VBA is a little rusty, but you might find something like this works.
To avoid VBA, and if you think your users might follow instructions, you could ask them to copy the date and then paste special->values to set the date so that it won't change in future.

Resources