Getting Started with Microsoft Excel Visual Basic for Applications (VBA)

Written by:  • Edited by: Michele McDonough
Updated Feb 28, 2010
• Related Guides: Microsoft | Visual Basic | Excel

It is nice to know that you can create code in your office suite to automate some repetitive tasks, but it may seem frightening to those who haven't tried before. No worries, here we explain Excel VBA basics.

A Tiny History of Visual Basic for Applications

BASIC has more than 20 years of history as a computer programming language. The name says it, it is “basic” and it is something like speaking with your computer. Later on BASIC evolved into Visual Basic, which you can use to design interfaces with your mouse. Meantime, Microsoft tailored Visual Basic to work with its Office suite and called it “Visual Basic for Applications”, which VBA stands for. In this article, we will touch the basics to give you a small step in getting going with your VBA programming experience. Even just a little knowledge of VBA can give a real boost of efficiency when working in Excel and other Office applications.

Getting Started with Visual Basic for Applications

Opening the VBA module is the same in all Microsoft Office programs, but I will choose Excel because we will have as much flexibility as we want with the calculations.

Microsoft Excel VBA Code Window
click to enlarge
Let’s go step by step from opening a blank workbook. Open up Excel. If you did not play with the preferences, you should have Sheet1, Sheet2 and Sheet3. You can start the VBA module by going through Tools -> Macro -> Visual Basic Editor or simply hitting Alt + F11. Now, we have our worksheet open and our code window. As you see, the window is split into three: at the top there are the menu and icon bars, on the left your VBA Project and on the right the code window. On the left pane you see “VBA Project(Book1)” and then “Microsoft Excel Objects.” Our default worksheets are listed as “Sheet1(Sheet1)”, “Sheet2 (Sheet2)” and “Sheet3 (Sheet3).” On the right, at the top you see two dropboxes: (General) and (Declerations). We will get to each one in a minute.

You do not need to stick with the default naming scheme of course. If you rename your file (your Excel workbook) to something else, such as “My_Workbook” and save, you will see that on the left pane VBA Project(Book 1) changes to VBA Project(My_Workbook). The same way, if you rename the worksheets, the changes will be applied to the Sheets under Microsoft Excel Objects. Let’s rename Sheet1 to Calculations, Sheet2 to Detailed_Reports and Sheet3 to Summary_Report. On the left pane in Visual Basic Editor, you will see that Sheet1(Sheet1) has changed to Sheet1(Calculations), Sheet2(Detailed_Reports) and Sheet3(Summary_Report.) However, if you switch back to your workbook and change the order of the worksheets, for example move Summary_Report to the left of the Calculations, the Sheet1, Sheet2 and Sheet3 notation will not change. The notation has an advantage: you can refer to the sheets as you name them. You do not need to say “Sheet1” in your code, go with “Calculations.”

Regarding the two dropboxes, there is such a thing as declaring variables in programming. I will try to keep things as simple as possible here, and open things up where necessary, to avoid losing you in the article. There are two types of variables: global and private. Global variables will be available in all modules in the code, whereas Private variables will only be available in the module it is defined under.

Also, there are the Sub and Functions. These are the two sections of code that perform a specific action. Function returns a value and Sub does not. We will come back to that later.

Read on for more details on Global Declarations, our first excel VBA application and making calculations.

Showing page 1 of 3

Comments

Showing all 2 comments
 
Tolga BALCI Jan 2, 2010 3:27 PM
Re: want to buy VBA textbook
Hello Adams,

Thank you for your comments.

I have to say that I have purchased many books from the Internet just by looking at the contents in the past and I could not benefit from any of them. One of the books I learnt many things was Excel Expert Solutions by Brian Underdahl. But for many of my friends the book was too complex.

I suggest you to go to a bookstore and spend some time to choose the book that is in line with your learning style. You can also make a search on Amazon.com on "Excel macros" and try to find one that you can follow easily.

Once again, the book that fits me is not guaranteed to fit you. That's why I cannot advise you the one that you can follow. However, if you are the type that works on projects and learn how to do things along the way, then you can go for the books I have told above.

Plus, don't be tempted to think that VBA is just for Excel. Once you learn how to do things, you can write VBA code in Word to extract data from Excel, or query an Access database, send the results to Excel, make the calculations and then receive the results back in Access. The possibilities are endless with Microsoft Office and VBA.

Once you choose your book and begin working on it, please come back to this article to share your experience with us.

Have a very happy 2010!

Tolga
Adams Taiwo Dec 31, 2009 8:36 AM
want to buy VBA textbook
Having gone through VBA, i now realse that i need to buy the book offshelf.

Pls give direction.
 
blog comments powered by Disqus
Email to a friend