Can you refer to an external macro with excel? - excel

I have a reasonably complex macro that I need to run on multiple different excel sheets, this macro is updated periodically and whenever a change is made its necessary to change it in each individual excel sheet. is there a way to get each excel document to refer to the one macro?
for example if i had a hierarchy like this:
DOCUMENTS:
-xlsheet1.xls
-xlsheet3.xls
-xlsheet2.xls
MACROS:
-macro1.bas
where there was a button in each sheet that ran macro1 when clicked.

I would recommend either moving that macro to your personal file or create an Add-In
Working with Personal File
Topic: Create and save all your macros in a single workbook
Link: https://support.microsoft.com/en-us/office/create-and-save-all-your-macros-in-a-single-workbook-66c97ab3-11c2-44db-b021-ae005a9bc790
Quote from the above link:
When you first create a macro in a workbook, it works only in that workbook. But what if you want to use the macro in other workbooks? To make your macros available every time you open Excel, you can create them in a workbook called Personal.xlsb. That’s a hidden workbook stored on your computer, which opens in the background every time you open Excel.
Creating an Add-In
Topic: Creating Excel Add-ins
Link: http://www.ozgrid.com/VBA/excel-add-in-create.htm
Quote from the above link:
I am often asked by users 'what is the best way to distribute their macros?' My answer, is without doubt via an Excel Add-in. After all, this is what Add-ins are for. For those that are not sure what an Excel add-in is, it's is nothing more than an Excel Workbook that has been saved as an Add-in, File>Save as \ Microsoft Excel Add-in (*.xla). Once saved and re-opened the Workbook will be hidden and can only be seen in the "Project Explorer" via the Visual Basic Editor. It is NOT hidden in the same way as the Personal.xls as this can be seen (and made visible) via Windows>Unhide.

The Personal file is good for having a macro across any number of workbooks on a single computer. In a networked environment with multiple users, you could simulate the Personal file by having a single workbook with your macros in it and coding all other workbooks to open and hide this workbook when they start up.

Related

Can I use VBA to tell if my spreadsheet was opened by a user vs. a linked document?

The Problem
I have an Excel workbook that prompts the user for some actions every time it is opened.
This same workbook contains charts that are linked in a PowerPoint file. These links were created by copying the charts in Excel and then, in the PowerPoint file, clicking "Paste Special", choosing "Paste Link", and selecting "Microsoft Excel Worksheet (code) Object" as the link type.
Every time the PowerPoint file is opened, I am prompted to update the links in the document.
The trouble is that, while PowerPoint is updating links, the Excel prompts that are intended for the user are shown and have to be clicked (multiple times) during the link update process.
Is it possible for my VBA code to detect when it's being opened for the purposes of updating links so I can skip the user prompts? I should note, in case it matters, that I'm using Microsoft Office 365 on Windows 10.
What I've Tried
I've tried monitoring the value of ThisWorkbook.ReadOnly, Application.Interactive, and Application.IsSandboxed during startup, but they all appear to have the same values regardless of whether the workbook is opened for the purposes of updating links or not. I'm not familiar enough with how link updating works to know what else to check for.
I've identified a couple of possible work-arounds:
First, if the Excel workbook is opened before PowerPoint is opened and the link update process starts, then the prompts don't appear. This will work, if there's no better solution.
Second, I could create a second "shadow" Excel workbook with charts generated from data in the first spreadsheet, but without user prompts. If PowerPoint links to the charts in the "shadow" workbook, then links are updated without issue. This is far from ideal because of the need to keep the two workbooks in sync.
I'm hoping for an elegant automated solution.

VBA - How run a Macro from another workbook without opening it?

I wanted to save all my VBA Project in a Excel workbook (or other type of file if possible) and then embbed it to run in another workbook. I've seen topics about it but only found ways to run the macros opening the first sheet.
I want to run a macro from a first workbook in a second one without opening the first workbook to do it. How can I do that?
Save the file with the macros as a xlam file and Excel can load them each time Excel opens.
You need to open File -> options -> add-ins.
At the bottom there is a button Go to (or something, I don't have English Excel on the current computer.)
Then add the file to the list by clicking Browse and finding the file you just saved as xlam file.
Two scenarios comes to mind:
1) You have a second macro for personal use and can save it locally (save in: %USERPROFILE%\AppData\Roaming\Microsoft\Excel\XLSTART). This will allow you to create quick-buttons for your macros, etc. These types of macros open with Excel and will be separate VBA Projects inside of the default VBA editor.
2) You have a network or drive that multiple users need to access, so each user has a macro in their file (.xlsm or .xlsb), where that internal macro reads Application.Run "filepath\workbookname.xlsb!macro", which also allows you to call a private subroutine (note that you could use Call, but Application.Run will ensure that even Private macros are able to be accessed). This shouldn't require the other workbook be open, though I have personally had one user whose computer always opens the other file, regardless.
Edit:
Third scenario (really 2b):
3) You have files where you want to regularly access another file... you will follow a similar approach to point 2 where you make a macro to Application.Run, though you can save that macro in your XLSTART folder... this will allow you to have a source macro location where others may also want to access and utilize. The source document would allow you to maintain 1 file for many users.
Your answers were great! Great to know about XLSTART folder from #Cyril, but based on #Andreas answer I found my path.
The "problems" with adding an Add-In as #Andreas said, are cause my VBA Project would be avaliable on the VB Editor to every workbook on that computer, and to run my macros I'd have to use Application.Run("workbook.xlam!Macro").
Then I found References, which I have the same features, including I can delete my .xlam file to remove my code, and don't have the problems I mentioned above.
Adding my VBA .xlam file as an reference, it'll be avaliable only to that specific workbook and I can run my macro just like it was on the same workbook.
For general knowledge:
ADDING A REFERENCE:
1- Save your project as an Excel Add-In (.xlam file)
2- Open your target workbook, go to the Visual Basic Editor
3- Go to Tools > References > Browse... find your .xlam file and make sure it's checked.
4- Done! Now you'll see the project on the editor and can run your macros just like it was on the same workbook.

How to refer to Excel macros (not functions) in .XLAM addin saved outside Personal Macro folder in Excel 2016/2019?

We can create an .XLAM addin with custom functions (UDF). Once we connect Excel to the addin, these UDFs can then be called from another workbook regardless of where the addin was saved (even outside the Personal Macros folder).
But the same cannot be done for macros (sub-procedures) saved in the same .XLAM file. From my research and trial it seems the sub-procedures can be called correctly if the addin was saved in the Personal Macros folder. But if the addin was saved somewhere else, the macro would not show up in the Macro list upon pressing Alt-F8.
Is there a way around this? We need the addin to be saved in C:\OneDrive\Macros\Addin.xlam because the addin would update frequently so this saves the hassle when everybody updates it automatically via OneDrive.
We are using Excel 2016 and 2019.
After several weeks of researching and trying out different methods, I found the best method as follows:
Make sure you saved your macro source (e.g. MyMacros.xlsm) as MyMacros.xlam so that the add-in contains the latest macros and functions.
Make sure MyMacros.xlam add-in is connected in Click File - Options (Or Alt - F, T) Add-Ins - Manage Excel Add-Ins - Go (G)
In the second Excel file (let's say MyExcel.xlsm), open VBA Editor (Alt-F11). Choose the MyMacros.xlam project and rename the name to MyMacros to be different (pic) In my example pics MyMacros is FreelensiaMacros.
If you have several XL files open, click on the MyExcel project in the left VBA Project List pane (not MyMacros!). Then go to Tools - References and select MyMacros from the list.
You should now see a new reference in the VBA Project panel on the left side (pic)
5., Create a module named something like CallMyMacrosMod and type in several macro names from the MyMacros.xlam file using Application.Run such as:
Sub FormatTables()
Application.Run "MyMacros.FormatTables"
End Sub
It is convenient to construct texts in the MyMacros.xlsm file itself storing the commands to call all of your macros. You can then copy and paste to Word (to avoid the quotes problem), then copy from Word and paste in this VBA Editor.
Save and close the VBA window.
You can now call the MyMacros macros from the macro list of MyExcel file without having to kep MyMacros open, simply by typing Alt-F8.
Save and close your XL file.
Sources:
https://www.myonlinetraininghub.com/calling-vba-in-add-ins-from-vba-modules
If the add-in is loaded, you can call any sub/function with the following code
Sub: Application.Run "YourAddinSub",param1, param2, ...
Function: Application.Run("YourAddinFunction",param1, param2, ...)
If needed, you can qualify the sub. For example: YourAddin.YouAddinSub

Excel Open 2 Workbooks at the same time

I have a workbook that I open with vba coding. There are several different userforms to input data and get reports out of this workbook.
It seems that while this workbook is open, and active, I cannot open, to look at and/or edit, an unrelated excel spreadsheet through Windows Explorer. One of my users asked me if while this program is running if she could look at (without closing the active workbook) a different spreadsheet. It has nothing to do with coding such a request into the active workbook, its more for convenience i.e. not having to close this one and open that one.
Is there anything I can do to facilitate this request? Is there some vba code I can use in my active workbook that allows or gives Excel permission to open more than one workbook?
I think you'll find it is when a Userform or any other Excel dialog-based object is open, then you cannot simply "double-click" on an another workbook to open, as Excel is busy and focus on the open dialog screen.
However, if you start another instance of Excel (Start -> All programs -> Microsoft Excel) and then open the workbook from within the new instance, then you work with additional workbooks.

VBA / Macro - How to avoid importing Module for Every file [duplicate]

I have a report that I run from a system once a week, and the data is exported into an excel document. I have set up the tool that exports the data to excel to do a decent job on formatting, but there is still more that I need to do once the data is in excel.
Is it possible to create a stand alone macro, save it in a Microsoft office folder and be able to call it after I open this file? I have written plenty of macros that are part of ONE given spreadsheet, but never tried one that was open to many spreadsheets.
IS it possible, and a starting direction of good resources of where I can learn how to achieve this goal? Thank you.
...and be able to call it after I open this file?...
You have 2 very good options
1) Write and save your macro to Personal file. See this link for more information
Topic: Deploy your Excel macros from a central file
Link: http://office.microsoft.com/en-us/excel-help/deploy-your-excel-macros-from-a-central-file-HA001087296.aspx
Quote from the above link:
Have you ever wanted to use that really handy macro in all of your Excel worksheets? You can. This column explains how to place your macros in a file called personal.xls and make them available each time you start Excel.
2) Create an Add-In
Topic: Creating an Excel Add-in
Link: http://msdn.microsoft.com/en-us/library/aa140936%28v=office.10%29.aspx
Quote from the above link:
You create a Microsoft® Excel add-in by creating a workbook, adding code and custom toolbars and menu items to it, and saving it as an Excel add-in file.

Resources