VBA Module disappears after saving Excel workbook - excel

I was just introduced to VBA. However, on saving my first excel file with VBA code, with .xlsm extension (I was advised to do so), and then reopening that saved file, I realized that all the VBA code had disappeared and the cells in which I had used the user-defined functions threw errors. Saving the workbook as .xlsx instead of .xlsm also didn't help. When I tried saving the VBA module, it showed that FUNCRES.XLAM is read-only and hence cannot be modified, and so try saving it at a different location. Doing so, saved an excel file which on opening showed a pop-up message that it's corrupt so can't be opened. Then I gave all permissions to FUNCRES.XLAM and it is NOT read-only. But the problem still prevailed. What should I do? Can someone please help me?
PS: It is Windows 10, Office 2016.

Your VBA code needs to be saved in a module within the same workbook in which you are working. This will probably be listed as VBAProject(Book1)
There will likely be other projects listed in the Project Explorer window of the VBA GUI, but your code will not get saved with those modules.
These are related to add-ins, and not to your workbook.

Related

create and distribute Excel add in

I have created an extensive macro in excel, that I now want to distribute. I decided I want to do this by saving the macro as add in to a shared network, where all of my collegues have access to it. This way maintaining the macro will be way easier, as I am sure that there are plenty of updates, and bugfixing to come.
I have never created an add in before, so I read several online tutorials + stack overflow questions about it. Up until now I always saved my macros in personal.xlsb, likewise with the macro I now want to save as add in.
My first attempt was to simply save the workbook, where I run the macro, as .xlam, which did not work (I guess because the macro was not saved in that workbook, but in the personal.xlsb).
So I deleted the macro from the personal.xlsb, opened a new workbook, opened the VBA editor, created a new module in that workbook and inserted my code. I then saved the workbook as .xlam. I checked that in the VBA Editor the IsAddin is set to True.
From here it gets confusing somehow. When I open the .xlam file there are no worksheets in it, the add-in is loaded, but the macro does not show up in "Macros", even though the code shows up in the Editor.
When
However the .xlam is still empty when I open it - no code to be seen.
When I open another workbook and load the add-in, in the VBA editor the add-in does show up, and the module with the code of the macro is there as well. But when I click on "macro" it does not show up.
When I open a different Excel file, the add-in is loaded as well (in Excel-add-ins the box is ticked), the module does not show up, and in the VBA editor the .xlam also does not show up.
I have saved, closed, opened, and restarted several times. I have checked if the add-in is saved anywhere else - it was, but I deleted all .xlam files (as non of them worked anyway), and repeated my attempts stated above. When I load the .xlam from the shared network it seems to save itself into the addin folder of microsoft (I guess that is what it should do when it is loaded).
I would really appreciate if someone could help me with how to properly set this up.
I have:
a VBA code that runs fine, when I run it as macro.
I want
to save it to a shared network as add-in, and get it to run.
Edit1:
the add-in shows up in the active add-ins section in options:

Excel 2010, why even a blank xlsm with blank module is causing VBA editor still show the workbook after it close?

Suddenly I have a weird problem with my Excel app.
I've looked the internet and in this site, found similar problem but it seems their problem is because of some codes. While mine, there is no code at all.
At first, after I close my xlsm work file (which has some modules with the code), then I open the VBA editor, sometimes it gives me message box "Out of Memory", sometimes it gives me "catastrophic failure" message box, sometimes there is no message box, but one thing for sure, in the VBA editor it still show the workbook which I've just close.
Strange thing, if I open the Excel app first (which gives me a blank worksehet by default), then I double click my xlsm working file, then close it, then open the VBA editor, there never any message box and in the VBA editor also there is no workbook which I've just close.
So it happen only if I directly open my xlsm working file... or any other xlsm file which has a module, even a blank module.
It doesn't happen if I directly open (by double clicking) an xlsx file.
Curious, I open the Excel app (it gives me a blank worksheet by default), Insert a blank module in VBA editor then save the workbook as Book1.xlsm.
.
.
.
Then I close the workbook.... and I see that in the VBA editor it still has the Book1.xlsm there.
.
.
.
Then I just close the Excel app, and open it again. Then I double click the Book1.xlsm.
Close the Book1.xlsm, there is no Book1.xlsm left in the VBA editor.
.
.
.
Then I close the Exel app. I directly double click the Book1.xlsm
Close the Book1.xlsm. Again, there is still Book1.xlsm in the VBA editor.
I've tried to repair the Microsoft Office 2010 via Control Panel ---> Programs.
After it's done, MS Office ask to reboot. So I reboot. Directly double click the Book1.xlsm, close the Book1.xlsm. No joy. VBA editor still show Book1.xlsm left there.
Although I realize that maybe the last resort I need to do is to uninstall MS Office and then reinstall it back, but still I wonder, if anyone here know what cause this kind of problem ? What cause a blank xlsm workbook with a blank module - after it close, the VBA editor still show it ?
Why if I open the Excel app first, then open the xlsm file, the problem does not exist ?
FYI, all the subs in my working xlsm file can still run without problem.
Any kind of explanation would be greatly appreciated.
Thank you in advanced.

View removed VBA code after repair of Excel file

I am getting an error when opening excel: We found a problem with some content in XXX. Do you want us to try and recover as much as we can? if you trust he source of this workbook, click Yes."
clicking Yes, "fixes" the issue but deletes a lot of VBA code, two weeks worth.
Whatever the issue it was introduced yesterday, I do not want to redo two weeks worth of coding. Is there anyway I can view what was removed, or open the VBA in notepad++ or something without opening the excel?
I opened another excel workbook and tried all the different options for the argument XlCorruptLoad in the Workbooks.Open to open the corrupt workbook. I noticed that there were two non existing sheets created in the project explroer of the corrupt workbook that had the code in there. I am not sure if it did that s a result of what I did or it was there all along and I did not notice it
Note that the reason I had a corrupted data is because the code was extracting a list and putting it in a cell validation formula..i guess I overloaded it.

Macro-Enabled Template keeps opening original document when macro is run

I made a series of macros on a document called 'Prac.xlsm'. I then saved the document as a Macro-Enabled template. When I run the first macro in the template it works but it also opens my original document titled Prac. The macro doesn't do anything to that original document though.
I tried to step through the templates macro and that works without opening up the original.
I then tried renaming the original file which resulted in an Excel popup saying "Sorry, we couldn't find ..file path & name for the Prac file.. Is it possible it was moved, renamed or deleted?" I can't see anywhere in the code where it refers to the file name Prac.xlsm. And because stepping through the code works without opening up the original file it has me stumped.
Any help would be appreciated.
Thank you.
Discovered it was because I was accessing the macros from the ribbon as I customized it. Unsure why but if I trigger the macros from the Developer tab they work.

Prevent a Save of Activesheet from saving a link to a module

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

Resources