Adding macro from one excel sheet to another - excel

I'm working with a list of addresses in excel 365. I want to perform a reverse geocode function to produce addresses from the available longitude and latitude info. to do this I have found an excel addin https://github.com/gramener/geocode-excel , which I downloaded. you can see an example in the screenshot above. the formula which it introduces is:
=#NominatimReverseGeocode(B32,C32)
Now what I want to do is make this available in my main sheet so that I can use the NominatimReverseGeocode. I've gone to File - Options - File - Add Ins
But the function 'NominatimReverseGeocode' does not show up in my main sheet. How can I make this function available in my main sheet?
EDIT:
I renamed the file to geocode.xlam in powershell. Now I'm getting:

If your intent is to make the Geocode functions available in all of your workbooks on your computer, do this:
Delete all previously saved Geocode.xlsm and Geocode.xlam files.
Download Geocode.xlsm.
Open it.
Go to File > Save As.
Change the file type from Excel Macro-Enabled Workbook (*.xlsm) to Excel Add-in (*xlam).
Click Save.
Close all instances of Excel.
Open Excel. (i.e. just the Excel application, so not opening a workbook.)
You should be on the Home screen.
Go to New > Blank workbook.
In the ribbon go to Developer > Excel Add-ins.
Check Geocode.
Click OK.
Close all instances of Excel.
Open any Excel workbook or make a new blank workbook.
Put =#NominatimReverseGeocode(51.5,-0.144) in any cell.
This should resolve to the Buckingham Palace Garden.
If your intent is to just have the geocode functions in one specific workbook, let me know and I'll write up the steps for that.

If you only need this code available for yourself and not other users by far the simplest way would be to create a personal macro workbook.
Create a workbook and save it as
C:\Users\username\AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.xlsm
Copy the code from the GeoCode workbook into PERSONAL.xlsm.
PERSONAL.xlsm will autoload with Excel. Prefix your function call with the workbook name: =PERSONAL.xlsm!#NominatimReverseGeocode(B32,C32)

Related

Is there a way to share a Excel VB Macro as a tool for all future workbooks?

I made a VB script that re-formats data exported to excel from a website so that it is compatible with a geocoder. As of now every export I have to go in, past the VB code run it and save the changes. I am wondering if there is a way to make the VB code available as a tool for all workbooks so it is just a matter of opening the xls file clicking a button and saving it?
To expand a little on BigBen's comment:
Open the Excel file with your code in. Open a new blank Workbook. Open up the VBA Editor (Alt-F11).
Drag the module which contains your code to the new workbook (in the left-hand navigator pane). You should see it add to this new Workbook.
Save you new workbook as MyFunctions (or whatever name you fancy) but choose the "Excel Add-in (*.xlam)" file type. You should see a new file appear in your folder as MyFunctions.xlam.
Back in Excel, from the File menu, choose Options (right at the bottom). And then Add-Ins from the left-hand list. At the bottom will be a drop-down box "Manage: Excel Add Ins". Hit Go...
You'll a list of the add-ins that your Excel knows about: some will be checked (that is they will be loaded at start-up) and some not.
Choose Browse, and navigate to wherever you saved MyFunctions.xlam. And double click on it. You may get a security warning (as your addin doesnt have a digital signature) but just go ahead and enable it (as you wrote the code: this is not blanket advice!). Close Excel (this remembers your choices).
When you next open Excel you should see your add-in file open in the VBA Editor, and your macros and UDFs available in whatever worksheet you are using.

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

How to open specific excel file from Blue Prism?

I recently got an assignment to create a robot in Blue Prism and I came to a problem.
I need to open a specific excel file and work whit it.
My application modeller is set to windows application, launched from an exe file. I can launch excel without any problems but when I'm using "Action->Buissnes Object(MS Excel VBO)->Open Workbook", nothing happens.
Xlsx is not opening.
If I go whit "Open Workbook" and after that "Get Worksheet as Collection" it actualy pulls data from a file to my collection, but again its not opening the file on screen.
What I'm doing wrong?
Should I create/open instances before?
How do they work?
To work with the Excel process you SHOULD use MS Excel VBO object. The sequence of Actions to Read an Excel file is as follows:
Create Instance: To start a session of Excel. It basically creates/open a blank Excel file.
Open Workbook: To open the required Excel file.
Show: To view the Excel file (it’s invisible by default). This steps in OPTIONAL.
Get Worksheet As Collection (Fast): To read the Excel file data and store it in a collection.
Close Instance: To gracefully close the Excel Instance.
First, in your object studio, you need to use action with "Open Workbook" object using MS Excel VBO. Next step will be another action with "SHOW" object from MS Excel VBO.
With first action, BP will open workbook, but it wont be visible on the screen all the data from that workbook will be available. Second action will actually show that workbook on your screen.
you can use show action from MS Excel VBO if you want to show your excel on screen. Open Workbook and get worksheet as collection is better way to get data from excel.
In MS Excel VBO one property available "Get worksheet name" select that option under the options 'get work sheet position' you can give worksheet position.for example u want to opened 3 sheet ,u will give 3.
To work with excel you should use Excel VBO and don't have to launch by defining application moduller.
Below actions from MS Excel VBO should work.
create instance
Open workbook
Show

Can you refer to an external macro with 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.

Resources