Chris Rae's VBA Pages

What is VBA?

For anyone interested in the above question (or a related one), here is a brief Beginners' FAQ that I have put together.

Q: What is VBA?
A: VBA (Visual Basic for Applications) is a programming language created by Microsoft. In a nutshell, it is a subset of their fully-fledged Visual Basic (VB) language designed for automation of applications – originally the Office 97 suite but now over 200 third-party packages as well. It is a slightly more complex language than VBScript (which is used for automating web pages, or simple batch processing tasks) but simpler than VB 6. It's quite different to VB.NET, the newest incarnation of VB.

Q: Why would I need to use it?
A: You'd use VBA to add functionality to a program. If you thought that Excel ought to have an "Insert/Large image of chicken" menu, you could just go ahead and create one. Perhaps you have a task you need to repeat regularly - you could write a macro so that just clicking a button will set it off. Be aware, though, that a lot can be done in the Office applications without using VBA. Remember that if someone else is likely to want a function too, there's a good chance it is in there somewhere. Whilst other languages (VB, for example) allow you to perform actions using the Office applications, VBA operates from within documents in the application itself. Instead of opening a document and then starting a separate program to perform the automated tasks, all of the programming can be encapsulated inside your document itself.

Q: Is it hard?
A: If you can program Visual Basic, it's easy as pie. If you can program in another object-orientated language, it's not difficult. If you know the Office suite upside down, it's doable. If none of the above apply then it's certainly not impossible but you'll have to accept that it's going to take a fair degree of work, a lot of which is going to be (ugh!) reading. Like a spoken language, there's a certain amount of groundwork that you rather have to read about - you'll be shooting yourself in the foot if you don't. Like any other language though, once you have learned the basics, you can start to pick things up as you go along.

Q: How on earth can a program you write in Word work in Excel, or PowerPoint, or Visio or something? That doesn't make a blind bit of sense. You don't know diddly about this stuff.
A: I'm sure that insulting yourself is one of the first signs of senility. Well, Chris (!), let me give you a vague example. If you wrote a program under Excel/VBA to count to three, it would work in Visio. If you wrote a program under Word/VBA to replace all occurrences of "ham" in a document with "eggs", it wouldn't work under any other application. A lot of VBA code is common between applications (displaying messages on the screen, repeating commands, looking in folders, etc.) but as soon as you start working with application-specific "objects", you've tied yourself to that application.

Q: What is a Word macro?
A: The distinction between a Word macro and a program written in VBA is exceedingly fuzzy. When you record a macro (using Tools/Macro/Record New Macro…"), Word actually writes a VBA program - you can see this program by going into the Visual Basic Editor (ALT-F11) and double clicking on the "NewMacros" module. If you know how to, you can then tweak this program to alter its functionality. Is this programming or recording macros? You be the judge. You could create fifty VBA programs without ever hearing of the VB Editor and you could equally easily write another fifty without recording a single macro.

If you have any suggestions for what ought to be mentioned on this page, please feel free to mail me. Either way, you may well want to head back to the index for my Visual Basic for Applications Pages.