Analysing Excel VBA/Macro code - excel

Is there a tool which will analyse/parse an entire Excel workbook and let us see all the macros, functions, VBA code?
I've been given a large number of Excel spreadsheets to analyse, we need to know what they do and how they do it, with a view to creating documentation, and/or re-writing. In many instances the original developers are gone and the users don't know the detail.

You can use MZ Tools which can give you some documentation generation abilities.
There's also some examples of how to do this yourself with VBA here

Additional resources for this question.
For office 2010 you can now use a VBA inspection tool that will alert you to upgrading issues such as deprecated Object model calls, Win32 calls, ActiveX controls that may/will require code changes.
Doesn't provide a complete output of all method and class names. So its not useful for commenting your entire code base, but it does provide useful upgrading comments.
Remediation Guide
Code compatibility Guide

Related

Anyway to obtain cell Precedents and Dependents in Excel Javascript API?

Unlike the desktop Excel APIs, which contain a "Precedents" and a "Dependents" on the Range object, the short answer is no for the Excel Javascript APIs, which brings me here.
Is there a way or third-party piece of code to circumvent this huge lack from Microsoft ? If I can't navigate the formula tree, there is just not much useful I can add in terms of add-ins for Excel Online.
Javascript API for Excel
Office Online
UPDATE: to add some context, I've been writing auditing add-ins in C# in the past, to evaluate formula complexity, perform advanced reconciliation of formulas, reconstruct formulas differently, etc... I'm trying to convert a small piece of it, as a javascript prototype add-in, to see whether Excel online can handle it.
I looked at this earlier in the year. The only way at the moment would be to parse out the references from the formula, which is non-trivial (but achievable) given the wide variety of possible reference types.
For a starter look here http://ewbi.blogs.com/develops/2004/12/excel_formula_p.html
And even then you would still have limitations for functions returning references like OFFSET, INDIRECT etc since the JS API does not currently have any Evaluate methods. And to be comprehensive you would also need to handle things like Implicit Intersection etc.
The latest version of the JS API does contain methods for handling Named Formulas, which is a good step forward so a parser would also be able to find references in those.
Why do you want to know a cell's precedents? Is it to implement formula-like functionality so that your add in can enable a function such as =JEROMESUM(A1,A7)?
If so, the JavaScript API has recently added this capability in preview for you to try, called "custom functions": https://aka.ms/customfunctions

Generating a tree in Excel

I have some data in an Excel sheet that represents a hierarchy. As the tree is large and reviewing the data is becoming quite a task, I am trying to generate a pictorial representation of the tree. Excel doesn't seem to have built-in support for tree generation. What would be the best way to generate the tree from within Excel? Are there add-ons available?
Suggestions related to tree generation using other tools would also be welcome. (I have already tried the org chart option in Visio. For some reason, it could not open my Excel file.)
There is a solution for both node-link tree or treemap. But it do require another tool since drawing this on Excel only would be very difficult if not impossible.
I propose to use the Funfun Excel Add-in which allows you to use JavaScript directly in Excel. With the capability of using JavaScript, you could make use the powerful libraries like D3.js or HighChart.js so that drawing tree chart would be much easier.
Here are two examples of generating trees in Excel.
I used D3.js in the first example and Hightchats.js in the second example. I made those two examples in Funfun online editor and then linked them to my Excel. You could check the code on the links below.
https://www.funfun.io/1/edit/5a36aaf145ac15144af3fe1e
https://www.funfun.io/1/edit/5925013604ce702ccfb22b0b
Here is what you need to do to link these samples to your Excel.
1). Insert the Funfun add-in from Office Add-ins store
2). Create a new Funfun or load a sample from Funfun online editor.
3) Have fun :)
Disclosure: I'm a developer of Funfun
Maybe you could use Excel Treeview control in a useform, you can find a great tutorial here. i have never used it personaly though.
Seems like VisualStudio 2010 allow you to do things a little smarter : you can have a look here. But i don't know enough this part of MS to help you anymore on this way.
And yet, the subject was discussed on another forum pointing to other third party tools.
I have used the ActiveX Treeview Control on Excel sheets, and it is fine. HOWEVER, depending on your version of Windows and which Security Updates you have, it may or may not work. I developed an application using one, for a major City bank, but we had to re-work entirely when certain upgrades were applied company-wide. After a very long and useless discussion with Microsoft in the US, we got nowhere at all. If you Google "excel treeview security upgrades" you'll find various discussions. I suggest using 3rd party, though I can't suggest one.

How to make a library I can access in VBA in Excel

I am doing a lot of similar tasks among some VBA scripts I am writing and would like to develop a library (a bunch of convenience functions using the typelib I'm working with) which I can call from all my various scripts. I am new to the VBA world and do not know how this is done and have had a surprisingly hard time trying to figure it out.
I think what I was looking for is an Add-In. Thanks everyone for your information.
Does this example help? It appears to also have a fix to a common issue when setting this up - You didn't mention which version of office - But 2003 is mentioned in that thread, so should be the fairly straight forward common case.
http://socko.wordpress.com/2008/06/01/vba-code-library/
Another option you have though it depends on the nature of your functions, is to create a COM object that you can call from your VBA scripts.
You can easily create a COM object using many languages including Delphi and VB (old style) it is also possible in .NET though a little more involved. You can then do your calculations in your COM object and even pass in the excel worksheet etc you wish to manipulate if required.
Depending on the nature of your functions this may or may not be useful.
You will need to use VB to do this. With Microsoft plug-ins to VB, you can manipulate Excel files without even opening them, much like you do now. The code will be very similar once you have the file open.
I would transfer all my code to VB and after its working like you have your VBA macros start making libraries out of the common stuff.
Lots of work, but if you really are doing a lot of this stuff, it will be great in the long run. (Job security too ;)
Check the comments here
http://www.dailydoseofexcel.com/archives/2004/10/15/code-libraries/

How to use VBA to automate several Office applications?

Although I've done VBA projects within a single application for both MS Access 2007 and Excel 2007, I haven't automated multiple applications at the same time. The generalized project is to open access, run some update queries that appends data to various tables. Then Excel needs to get the data. Some formating changes are needed in Excel, such as grouping that doesn't automatically change the date range. Finally, I plan to build it out such that the excel file will be emailed automatically.
Some parts of this are clear how to accomplish it, such as Excel will be getting the data by ODBC from Access. But where should the master VBA live? I could have a button in Access that would start running Access VBA, but is it a good practice to have the Access VBA start manipulating Excel? Does that make it difficult to debug?
To get started from Access, add a reference to the Excel object library. Then use the object browser to familiarize yourself with how the Excel object hierarchy looks from within Access. It is going to be somewhat difference, because the top-level object in Excel code is implicit (as it is in Access), and has to be explicitly referenced when coding in Access.
The Access Developers Handbook has excellent chapters on automating the rest of Office from Access.
Last of all, it's best once you've coded using the reference to the other app's automation library to help you program, you want to switch to late binding so you can remove the reference. This means not using any of the external library's specific data types (you mostly use plain object variables) and using none of the constants defined in the external library. My production code with late binding usually includes the early binding version commented out, alongside the late binding version.
I think the keywords you are looking for is "microsoft office automation".
Make an application in you favorite programming language that supports COM interfacing and then use automation to do the manipulations in the different office applications.
Look here c# How to access an excel cell? and here How to read data of an excel file using c# ? and Google.
I have recently done something quite similar to this, and have found that I can output HTML with built-in CSS for formatting that loads quite nicely into Excel. I used Access to allow users to build their required output, only opening Excel to display the results. You may find that HTML output makes for nicer emails.
Do all the work in Access VBA. See the following URLs for some sample code
Modules: Sample Excel Automation
Modules: Transferring Records to Excel with Automation
Also note that if you are dealing with multiple versions of Excel late binding becomes a necessity. Late binding means you can safely remove the reference and only have an error when the app executes lines of code in question. Rather than erroring out while starting up the app and not allowing the users in the app at all. Or when hitting a mid, left or trim function call.
This also is very useful when you don't know version of the external application will reside on the target system. Or if your organization is in the middle of moving from one version to another.
For more information including additional text and some detailed links see the Late Binding in Microsoft Access page.
As far as emailing goes there are a number of options at the Microsoft Access Email FAQ
You may put the code in Access, or Excel. In my experience, that is easier to work with than splitting the code between the two (which also works).
If you find that the automation code runs too slowly, you can split the code, so that Access is run by functions in Access, and Excel from functions in Excel, and the master code just runs routines in both. In this case, you can put the master code where-ever you get the nicest user interface for starting things. I've used C,Access,Word and Excel, and where you put the start button doesn't matter a whole lot if all the code is somewhere else.
If your users or maintainers are more familiar with one application, you may wish to put your main or master code there, but more often I find it is better to put the main or master code with the guts of the application, so that I can have no code at all in the other partner.
From the brief description, it sounds like more work will be done in Excel, so I would put all the code there.
Pulling the data out of Access doesn't even need an Access Application object - a DAO or ADO or ODBC object will work (or even DDE), which will be much faster, much more robust, much easier, much better than having both applications open and automating one from the other.
If you start from Access, it sounds like this project will require an Excel automation object. That works well now, but it is still much slower and more fragile doing it with just Excel and a DAO object.
I haven't done VBA for ages although Access was my entry-point into the world of programming. Moving from Access 97 to Visual Basic was easy and I remember that I wrote a lot of stuff using Access more like a VB-form for doing many kinds of tasks (not necessarily database-stuff).
This is the reason why I think you should stick to Access and from there, with help of VBA, do your stuff in Excel etc..
Good luck

Building a user interface to an Excel model

I built an excel model used to analyze real estate transactions. I would like to create a user interface to overlay the model so that the file can be distributed to clients to evaluate potential investments
The interface will serve two primary functions:
1)Enhance the user experience by creating an easy to follow input page. The entered data will then flow through to the model from which reports will be generated for the user to view.
2)Protect the intellectual property of the model by restricting the user from the underlying model. The user will not be able to view or edit the formulas in the excel file.
I believe this can be done using MS Acess/Visual Basics but I was hoping to find a program that is more professional looking. Can anyone suggest a program/programming language in which this type of user interface could be created?
Thank you in advance for your help.
You don't have to use Access, VBA (Visual Basic For Applications) is built right into Excel. There are good books available and lots of web resources. Your main problem seems to be the UI is not professional - I'd disagree, out of the box it will look like an application developed 5-10 years ago, but it will still work and look ok.
If you want a really slick interface, I'd suggest you look at WPF and integrating that into Excel. However there is a big learning curve to get that going, I'd get a basic UI going, show/sell that to your customers, and then ask them what they want.
I think my first question would be how savvy are you with development? You really didn't elude to that in your post.
If your using Excel 2007, there are ways to hide menu commands in Excel and remove a lot of the current interface and replace it with your own. It looks like Excel and functions like Excel, however, the ribbon interface looks vastly different. The other side is just to add a Tab or Menu and have your commands there. It sounds like you want use Excel as your UI, but replace the commands with your functions.
If you want to HIDE your IP, and you decide VBA is the way to go, then your code is stored inside the Excel Spreadsheet and is accessible to anyone who knows how to open the VBA editor.
If you decide to do it in .NET, then your functionality exists in an assembly outside Excel, and the .NET runtime loads it when you open the Spreadsheet. No amount of poking around inside Excel will help them find your "code".
There are lots of code samples to help you with both. I would recommend VB.NET if you decide to do .NET. From a pure syntax point, VB.NET handles lots of the office programming "quirks" well and can help you speed development. Here is the office development site, that you can go and get information before making a decision.
Also, according to this article you need to buy Visual Studio Professional 2008 or 2005 to get the graphical designers and such. If this isn't feasible or affordable, VBA might not be a bad idea to start with.
In regards to my development savvy, I am novice. I am familiar with the basics of VB but doing the actual development for the type of UI I have in mind is beyond ability. I plan to hire a developer to create the UI but before I can do that I need to decide the language it will be built in.
MrTelly, Chris, I appreciate your advice.
I am currently in the search for a skilled developer willing to collaborate to determine the best language to build the UI and then execute the development. If you have any recommendations or would like to discuss the project yourself I can be contacted at phtwright#gmail.com .

Resources