VBA for Beginners
An Introduction to Learn VBA Programming with Tutorials and Hands-On Examples
Text Copyright Light Bulb Publishing
All rights reserved. No part of this guide may be reproduced in any form without permission in writing from the publisher except in the case of brief quotations embodied in critical articles or reviews.
Legal & Disclaimer
The information contained in this book and its contents is not designed to replace or take the place of any form of medical or professional advice; and is not meant to replace the need for independent medical, financial, legal or other professional advice or services, as may be required. The content and information in this book has been provided for educational and entertainment purposes only.
The content and information contained in this book has been compiled from sources deemed reliable, and it is accurate to the best of the Author's knowledge, information, and belief. However, the Author cannot guarantee its accuracy and validity and cannot be held liable for any errors and/or omissions. Further, changes are periodically made to this book as and when needed. Where appropriate and/or necessary, you must consult a professional (including but not limited to your doctor, attorney, financial advisor or such other professional advisor) before using any of the suggested remedies, techniques, or information in this book.
Upon using the contents and information contained in this book, you agree to hold harmless the Author from and against any damages, costs, and expenses, including any legal fees potentially resulting from the application of any of the information provided by this book. This disclaimer applies to any loss, damages or injury caused by the use and application, whether directly or indirectly, of any advice or information presented, whether for breach of contract, tort, negligence, personal injury, criminal intent, or under any other cause of action.
You agree to accept all risks of using the information presented in this book.
You agree that by continuing to read this book, where appropriate and/or necessary, you shall consult a professional (including but not limited to your doctor, attorney, or financial advisor or such other advisor as needed) before using any of the suggested remedies, techniques, or information in this book.
Table of Contents
1. Introduction
Microsoft Excel is a spreadsheet application developed by Microsoft and is a part of Microsoft Office package. It is available for Windows, Linux, macOS, iOS and Android operating systems. VBA , a short for Visual Basic for Applications is an event driven programming language used within Microsoft Office products such as Microsoft Word, Microsoft Excel, Microsoft Access, etc . to perform certain tasks. Beyond MS Office, VBA is also supported by software products of other companies such as AutoCAD, LibreOffice, WordPerfect, CorelDraw, etc .
Visual Basic for Applications is based on Microsofts event driven programming language Visual Basic 6 . In 1998, stable version of Visual Basic 6 was released and support for the same was discontinued in 2008. However, VBA remained popular and Microsoft kept working on it. In 2010, VBA was upgraded to Version 7 (VBA 7). The latest stable release Version 7.1 happened in 2013 and is supported by Microsoft Office 2013 (or Microsoft Office 15) and MS Office versions released after MS 2013 . The latest version of MS Office at the time of writing this book is Microsoft Office 2019 .
2. Scope
VBA applications can be written inside most Microsoft Office products. However, as the title of this book suggests, we will only learn to write VBA applications for Microsoft Excel. This tutorial is meant for someone who is comfortable with Microsoft Office, especially with Microsoft Excel. Some previous programming knowledge is preferred but not required. Since VBA is an event driven programming language, if you are totally new to programming, the learning curve will be slightly steeper for you. If you have good programming experience, you will really enjoy this book and if you know Visual Basic, learning VBA will be an absolute cakewalk.
This book will teach you the basics of VBA for Excel and in the end, you will be able to write simple VBA applications for Microsoft Excel.
3. Getting Started
To begin learning VBA, you should have Microsoft Excel installed on your computer (VBA is not supported on mobile versions for MS Excel). Microsoft Excel is shipped with Microsoft Office. If you do not have it installed on your computer, you will have to purchase it (Visit https://products.office.com/en-us for more information). At the time of writing this book, a month long trial version of Microsoft Office is available at
https://products.office.com/en-us/try .
If you already have Microsoft Excel, make sure that the Version is 2013 or later. We will be using Microsoft Office 2016 on a Windows machine for demonstrating examples in this book. If you have Microsoft Office 2013 or later on Windows, Linux or macOS, the examples demonstrated here will work just fine. So, let us get started!
Open Excel and enable Developer mode . To do so, click File -> Options , select Customize Ribbon .
Under Main Tabs Ribbon , check Developer and click OK .
A Developer tab will now appear on the main ribbon, click that.
VBA programs will be written in an application called Visual Basic editor . To open it, click Visual Basic under the Developer Tab. Alternatively, you can hit Alt + F11 while a spreadsheet is active to access Visual Basic editor. It shall look something like this:
If you have followed so far and managed to launch the Visual Basic (VB) editor application as shown above, you are good to go!
3.1 VBA Program Demonstration
Excel spreadsheet is where you will be adding GUI components (such as buttons) and VB editor is where you will be writing the programs. Let us write a simple program to display text in a Message Box after the user clicks a Button . You need not understand any of this; you will learn all the steps mentioned in this chapter step by step as this book progresses. For now, simply follow the procedure. With this, you will learn how to execute a program and perhaps appreciate the beauty of VBA programming and what it can do within Excel.
Under Developer Tab, click Insert -> Command Button (under ActiveX Controls )
This will give you the ability to draw a Command Button anywhere on the spreadsheet. To do so, determine the area on the spreadsheet where you want to draw the button, hold down left mouse click, drag the cursor to draw and release the mouse button. This will draw a Command Button as shown below:
Let us set this buttons properties. Right-Click on this button, click Properties .