I created an Excel .xlsm file with a bunch of functionality in some VBA Modules/Classes and now I've decided to separate out the code because it will be shared across 4 different sites.
I saved the vba_code.xlsm to a shared location and created my 4 different siteXYZ.xlsm files.
In each siteXYZ.xlsm file I would go to the "Tools | References" menu in the VBA editor and add a reference to the shared vba_code.xlsm at a shared file location \share_location\fileLocation\vba_code.xlsm
At this point, I tested out the functions in siteXYZ.xlsm and everything would work fine.
However, every time I saved siteXYZ.xlsm, closed Excel and then reopened the file it would lose the reference to my vba_code.xlsm file.
How can I keep the VBA references saved with my siteXYZ.xlsm file?
After spending hours searching for an answer and trying various methods such as adding a digital signature to the vba_code.xlsm file and trying to programmatically add the reference on Workbook_open I found a forum post describing the problem:
My siteXYZ.xlsm file had no VBA code or macros defined within it so Excel refused to save the VBA Project and as a result did not save the VBA Reference to vba_code.xlsm.
The solution was simple:
Add ANY VBA code to the siteXYZ.xlsm and save it.
I just double-clicked ThisWorkbook under the VBA editor and added a function to Workbook_open that doesn't do anything:
Private Sub Workbook_open()
End Sub
The usual method of achieveing this is by saving your vba_code.xlsm as an addin (XLA or XLAM) and storing it in the shared location, then adding the addin to Excel in your 4 different sites. You can also extend this approach by using your own Addin Loader instead of Excel's. There is a working example of an Addin Loader at
http://www.decisionmodels.com/downloads.htm
Following #nvuono
You need to add some kind of module/reference to the excel file for it to save the references you have added.
Private Function addJunkModuleToGetReferencesToSave(ByRef wb As Workbook)
Set new_module = wb.VBProject.VBComponents.Add(vbext_ct_ClassModule)
new_module.name = "Junk"
new_module.CodeModule.AddFromString ""
End Function
Related
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.
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
I have an Excel / VBA question. I've come across a circumstance where VBA is being used to save a large number of activesheets as individual workbooks. Itself not an issue. However in the workbook they are being saved from there is a module that needs to be in each of the new individual files. The save method is saving a link to the module and not the module itself.
I'm looking for method of saving the activesheets as workbooks and forcing the module to be included in each of the new files. Unfortunately I'm not allowed to show code references for this but any experience someone may have with this type of issue would be appreciated.
All the files are being save as type .xlsm and also save the format is set to Macro Enabled Workbook
The Excel Version is 2013
I have a very complex Excel workbook, with an awful lot of VBA code and macros in it. For version control purposes, I'd really like to be able to save the code as separate files to the workbook proper - to split it off into modules. Is this a thing that's remotely possible?
You should also look into Add-ins (.xlam). These allow your modules/class modules to be referenced in other workbooks. If you deploy a tool to a workgroup then realize that you need to change a macro you can update the .xlam and it gets pushed to all workbooks that reference it.
I'm trying to get a simple VBA function to run in an excel (.xlsm) worksheet.
I created this function:
Function abc()
abc = 2
End Function
in Module2, and it worked.
But after I copied the spreadsheet to another system, it now just shows "#name" as if it can't find it. The function shows up on the available list of functions however.
This makes me think there is some kind of setting I need to enable, but I've enabled whatever I was prompted for. Any ideas?
I should've known it was in the trust center:
http://office.microsoft.com/en-us/excel-help/change-macro-security-settings-in-excel-HP010096919.aspx
Basically, hit the ball and poke around until you find "trust" and "enable macros" and select the least secure options.
And then close and re-open the spreadsheet.
I think we may need more information. What version of Excel are you using? Is it different from the destination system version?
You'll want to make sure that Module2 was in the Workbook you copied into the other system. Excel 2007 stores macros on a personal workbook by default sometimes so you'll need to check the the code is actually inside of the .xlsm file.
If you're still stuck and need a quick fix just copy the code text into the new system's Excel workbook directly without making a .xlsm file (create new module in the other system then paste).
If you would like to learn how to put together add-ins you can get started here or here.