I have about 8 different workbooks that all have the same modules in them. Unfortunately, the workbooks need to be separate. I have four modules in each workbook. Whenever someone finds a problem with the workbook such as an error I did not catch originally in the code, I have to make the correction to all of the workbooks. Presently, I make the correction in one of the modules, export it, and then delete the bad module from the other workbooks and import the corrected module(s). This is time consuming and I am hoping to find a way to either quickly import the updated modules or access the modules from a centralized location on the network drive. Is this possible?
I have scoured the internet but either I am not typing in the correct search or no one seems to have this issue. Thanks
If all of these workbooks reside on your machine and are used mostly by you, then you could put the macros in your personal.xls file and have only one copy on any of the various spreadsheets. There's a good tutorial about the personal.xls on Ron de Briun's site:
http://www.rondebruin.nl/win/personal.htm
Part of your question suggests that the workbooks might reside on other computers and you are then updating and distributing the new versions. If that is the case you might try using the method suggested by Charles Williams in this Stack Overflow question:
How to get VBA excel addin .xlam to replace itself by a remote updated .xlam?
Then you would simply create a shared add-in and replace the file in one location referenced by the xla/xlam. Ozgrid has a decent primer on creating add-ins:
http://www.ozgrid.com/VBA/excel-add-in-create.htm
Hope that at least some of this helps with your problem.
Related
I have several workbooks that have similar and reused VBA code sometimes I need to update the code and I would like to do this in one place instead of in 20 different workbooks is this possible?
If not is there a way to copy all the macros from one workbook to all the others after a change has been made?
Yes, you can reference other workbooks from VBA. Might be easiest to put it in an addin though.
If you are using class modules, then you can only set them as either private or public not createable (something like that). Obviously they'll need to be public, but you can get around the inability to create them by building simple helper functions in a normal module that do nothing other than create a new instance of the class and return it.
It is possible, yes.
This answer on SU mentions one possibility that is explored more in-depth in this support article.
If you find yourself recreating the same macros, you can copy those
macros to a special workbook called Personal.xlsb that is saved on
your computer.
By default, when you create a macro in Excel, the macro works only in
the workbook that contains it. This behavior is okay as long as you
don’t need to use that macro in other workbooks. Any macros that you
store in your personal workbook on a computer become available to you
in any workbook whenever you start Excel on that same computer.
In short: record a macro and choose to save it in Personal Macro Workbook. Save and exit, then re-open Excel. Go to the View-tab and click unhide.
The support article gives a more detailed step-by-step.
Sure is possible,
the two ways that I know are:
copy your macros on Personal.xlsb (like Vegard wrote) or (it's my usually case because I've also my custom Ribbon to play all my custom cmd) you can open a new excel file, write in this file all your macro/function/class.... and then save it as xlam file.
After you need to go on excel option, add components and choice the xlam file before created.
Tips, in the xlam file use a specific macro names and not macro1, macro2,.... because into your next files will be easy to create more macro and use the same name is usually..
I'll add my answer based on experience as it seems the ones given are all (unspoken) focused on being "free".
I started out using a workbook as my code library (as suggested above), but every time I made an adjustement or improvement to those code snippets, I had to go back to the other workbook and update it etc. This got old fast.
Once I started developing a bit more professionally, it made sense to invest in a tool to manage my code.
I use MZTools which also comes highly recommended by various Excel MVPs such as Ken Puls, Ro Bovey, etc. but there are other ones out there. Usually these tools also provide addtional functionalities that are useful to a developer. Invest a few bucks if you want to save your self from a headache.
I've created an Excel 2010 workbook with a number of sheets. VBA code that performs various data manipulations is in a couple of modules, and also attached to a couple of forms. The workbook is being distributed to a couple dozen people in different departments to use. They will be populating their workbook with their own department-specific data.
If I need to distribute an update to the code (either a bug fix or some new function), how can that be done? I don't want the users to have to reenter or copy/paste all their data into the 'new' workbook - I'm essentially looking for a method to update the VBA Project that's inside their existing workbook.
You could create an additional helper workbook Help.xlsm, that has its file attributes set to read-only.
Move all the vba code, that you might need to change in the future, into Help.xlsm
The file that you distribute then needs a reference adding to Help.xlsm
Now in the future changes can be made to Help.xlsm and they should appear in the client's files.
Above assumes all your customers are on the same network so that you can store Help.xlsm somewhere that is accessible to all their files.
This article explains it better than me:
http://www.excelguru.ca/content.php?152-Deploying-Add-ins-in-a-Network-Environment
You would need to export the modules and forms and manually import them into the existing workbooks. I used to have to do this for some projects i worked on.
Alternatively you would need to write some helper code to import the old data into a newly published workbook, but this depends on how the data is organised of course. Again this is another approach I took for a different project.
You can also do this procedurally. Ive used this for small patches.
http://www.cpearson.com/excel/vbe.aspx
Is it possible to have modules be external to the actual Excel file and call the functions/subs within them externally?
My thinking is if there are multiple Excel files that use the same module, instead of updating each one of those files separately when I make a change, can I just update the one module stored on a server or something?
I have doing something like you describe for years. You can move your VBA code to a VB6 ActiveX dll, organize it into classes, and load that dll as a reference from Excel VBA.
This is a good way to reuse non-workbook specific code. For instance, I have code that queries a mainframe. I like to call it from Excel, but the details of the connection and how data is passed are contained in a dll that I can load from Excel, VB6, Word, .NET, wherever. I have a similar dll for reading data from AutoCAD drawings, one for interfacing with a product DB on a MySQL server, etc.
The code that remains in Excel tends to be simple formatting stuff. Say I return a variant array of strings (technically a COM SAFEARRAY) from some library that I wrote. I would then output it into Excel, maybe do a text-to-columns, and have a list of results returned to the user.
You can also pass and return more complex data structures. The beauty of VB6/COM Automation (and I didn't appreciate this until I learned to do it the harder way in VB.NET or C#) is that the data will flow in and out of your components seamlessly and all the necessary interfaces will be created for you.
The main change to your code will be replacing things like ThisWorkbook or ActiveSheet with explicit parameters like (Byval sht as Excel.Worksheet). These will be caught at compile time in VB6 (since it doesn't know what ThisWorkbook is), so you cannot overlook them; you are forced to pass an explicit reference.
I also notice that my code inside the dll becomes more paranoid if it receives a Worksheet or other Excel object as a parameter. In VBA you might have had more assurance that you were passing a good object since it was private to a given workbook. The dll does not know who is calling it, so I view the passed-in object with more suspicion (check if Nothing, sheet name, formatting clues to ensure I am using what I think I am using).
The only downside I see is that you will have to get a copy of Visual Basic 6.0. I bought mine in 1998. It is no longer available from Microsoft, but surely there is someone out there who will sell it to you. The latest service pack is SP6.
You will also have to become familiar with "regsvr32" and "regsvr32 /u" to deal with the "ActiveX can't create component" errors as you open your workbooks on various computers. I just publish my dlls to a mapped network drive and re-register them on the user's computers whenever there is a significant change. Obviously this is a intranet/single company solution. Publishing the updated versions is much more of a pain the farther you are distributed.
Not sure if this would satisfy your needs, but you could create your common module as an "add-in" and so install it so that all files that you open in the same instance of excel would have access to the add-in code.
It would not be my recommended way of doing it because I would be worried about suitable testing of all the excel files that use it, when you make a change, plus the added complexity of getting users to install your add-in (this may not be an issue for you). I have a "developersToolkit" module I use across 8 different Workbooks, but I import the module into each workbook so its stand alone and I can also test changes for compatibility with each of the 8 workbooks.
I have an Excel template that I use to keep information about various clients. These clients can be in various stages of the process such as "submitted", "pending", etc. The status of the client will determine in which folder the information spreadsheet is saved (i.e., there's a folder for all "pending" clients, a folder for all "submitted" clients, etc.). I was wondering if anyone knew if there was a way to write a Macro that could "read" information from a 'Status' cell and, depending on what was saved in that cell, save the Excel sheet to the correct folder?
I agree with the comments: the macro recorder is the easiest way of learning the syntax of commands that you can use from the keyboard. However, I do not believe it will help here.
When the macro checks the status cell, it needs to close the client workbook (I assume it has already been saved) and then move it from its current folder to the new one. I do not believe there is any way to get the macro recorder to record the statement that moves a file from one folder to another.
Small, easily written, macros can be used to perform a wide variety of tasks. You need to have some pretty advanced requirements before Excel VBA will not be adequate. I recommend everyone learns VBA. Search the Internet for "Excel VBA Tutorial". There are many tutorials and most are free. Try a few then complete one that matches your learning style. Alternatively, visit a good library or bookshop where you will find a selection of VBA primers. I recommend visiting the library and trying a few primers before visiting the bookshop to buy the one most suited to your needs. Learning VBA will not take too long and the investment in time will quickly repay itself.
Your macro will have to be in a master workbook which accesses and updates the client workbooks. Many of the answers here show how to work with multiple workbooks. Try a few search terms and I am sure you will find something that gets you started.
Look up "FileCopy Statement" This is the simpliest way to copy a file from one folder to another. The Move method which involves use of FileSystemObjects is more complex but more flexible.
Good luck and welcome to the joys of programming.
Currently I am creating VBA Macros for Excel and recently have started using UserForms.
However, the dilemma is once the Excel File is deployed to the users, there comes a time that I need to update the code on the UserForms or Modules.
What I would like to know is: Is it possible to automatically update these UserForms or Modules by having Excel go online and download the updated UserForms and Modules like as patcher or something?
I got this idea from this link
I think you will find this thread useful, it suggests ways to update an Excel add-in. I'd go down the route of getting the Excel file to update a separate small bootstrap file, that then handles any necessary update (suggestion by Robin Hammond on the thread).
Where I work, we solved this dilemma by storing the workbook on the network as a read-only file which is always used as the point of entry.
If the user wants to save it, they can do so on their own machine. If you want to update the file, you just switch out the current one, since it will not be locked (due to being read-only). Obviously, this approach only works if you have a shared network, but I think it's a rather simple to implement approach.