VBA Part 1: Introduction
In this tutorial you will learn how to create real-world macros. The focus is on learning by doing. This tutorial has coding examples and activities to help you on your way. You will find a quiz at the end of the tutorial. You can use this to test your knowledge and see how much you have learned.
In part one of the tutorial we will concentrate on the basics of creating Excel macros. See the next sections for the learning outcomes and for tips on getting started with VBA.
Learning Outcomes
- Create a module
- Create a sub
- Understand the difference between a module and sub
- Run the code in a sub
- Write a value to a cell
- Copy the value from one cell to another
- Copy values from one range of cells to another
- Copy values between difference worksheets
- Test your output using the Immediate Window
- Write code faster using the With Statement
- Create and use variables
- Copy from a cell to a variable and vice versa
Before we get started, let’s look at some simple tips that will help you on your journey.
6 Tips For Learning VBA
- Practice, Practice, Practice – Don’t try to learn by reading. Try the examples and activities.
- Type the code examples instead of copying and pasting – this will help you understand the code better.
- Have a clearly defined target for learning VBA. One you will know when you reach.
- Don’t be put off by errors. They help you write proper code.
- Start by creating simple macros for your work. Then create more complex ones as you get better.
- Don’t be afraid to work through each tutorial more than once.The more times you do it the more deeply embedded the knowledge will become.
Basic Terms and What They Mean
Excel Macros: A macro is a group of programming instructions we use to create automated tasks.
VBA: VBA is the programming language we use to create macros. It is short for Visual Basic for Applications.
Line of code: This a VBA instruction. Generally speaking, they perform one task.
Sub: A sub is made up of one or more lines of code. When we “Run” the sub, VBA goes through all the lines of code and carries out the appropriate actions. A macro and a sub are essentially the same thing.
Module: A module is simply a container for our subs. A module contains subs which in turn contain lines of code. There is no limit(within reason) to the number of modules in a workbook or the number of subs in a module.
VBA Editor: This is where we write our code. Pressing Alt + F11 switches between Excel and the Visual Basic Editor. If the Visual Basic editor is not currently open then pressing Alt + F11 will automatically open it.
Sub TestSub
Dim TestVar As Integer
TestVar = 1
Debug.Print TestVar
End