Excel Programming Language – The Ultimate Guide to Macros in Excel

Excel Programming Fundamentals

Following are the top 10 Excel programming fundamentals that you need to learn to get started.

1. VBA Procedures

Procedure in VBA is a set of codes or a single line of code that performs a specific activity.

  1. SUB: Sub procedure can perform actions but doesn’t return a value (but you can use an object to get that value).
  2. Function: With the help of the Function procedure, you create your function, which you can use in the worksheet or the other SUB and FUNCTION procedures (See this: VBA Function).

2. Variables and Constants

You need variables and constants to use values in the code multiple times.

  • Variable: A Variable can store a value, it has a name, you need to define its data type, and you can change the value it stores. As the name suggests, “VARIABLE” has no fixed value. It is like a storage box that is stored in the system.
  • Constant:‌ A constant also can store a value, but you can’t change the value during the execution of the code.

3. Data Types

You need to declare the data type for VARIABLES and CONSTANTS.

define data type

When you specify the data type for a variable or a constant, it ensures the validity of your data. If you omit the data type, VBA applies the Variant data type to your variable (it’s the most flexible), VBA won’t guess at what the data type should be.

Tip: VBA Option Explicit

4. Objects, Properties, and Methods

Visual Basic for Applications is an Object-Oriented language, and to make the best out of it; you need to understand Excel Objects.

The workbook you use in Excel has different objects, and with all those objects, there are several properties that you can access and methods that you can use.

5. Events

Whenever you do something in Excel, that’s an event: enter a value in a cell, insert a new worksheet, or insert a chart. Below is the classification of events based on the objects:

  1. Application Events: These are events that are associated with Excel application itself.
  2. Workbook Events: These are events that are associated with the actions that happen in a workbook.
  3. Worksheet Events: These events are associated with the action happens in a worksheet.
  4. Chart Events: These events are associated with the chart sheets (which are different from worksheets).
  5. Userform Events: These events are associated with the action happen with a user form.
  6. OnTime Events: OnTime events are those which can trigger code on a particular point of time.
  7. OnKey Events: OnKey events are those which can trigger code when a particular key is pressed.

6. Range

The range object is the most common and popular way to refer to a range in your VBA codes. You need to refer to the cell address, let me tell you the syntax.

Worksheets(“Sheet1”).Range(“A1”)

7. Conditions

Just like any other programming language, you can also write codes to test conditions in VBA. It allows you to do it in two different ways.

  • IF THEN‌ ELSE: It’s an IF statement that you can use to test a condition and then run a line of code if that condition is TRUE. You can also write nesting conditions with it
  • SELEC‌T‌ CASE: In select case, you can specify a condition and then different cases for outcomes to test to run different lines of code to run. It’s a little more structured than the IF statement.

8. VBA Loops

You can write codes that can repeat and re-repeat an action and in VBA, and there multiple ways that you can use to write code like this.

  • For Next: The best fit for using For Next is when you want to repeat a set of actions for a fixed number of times.
  • For Each Next: It’s perfect to use when you want to loops through a group of objects from a collection of objects.
  • Do While Loop: The simple idea behind the Do While Loop is to perform an activity while a condition is true.
  • Do Until Loop: In the Do Until, VBA runs a loop and continue to run it if the condition is FALSE.

9. Input Box and Message Box

  • Input Box: Input Box is a function which shows an input box to the user and collects a response.
  • Message Box: Message Box helps you show a message to the user but, you have an option to add buttons to the message box to get the response of the user.

10. Errors

Excel has no luck when it comes to programming errors, and you have to deal with them, no matter what.

  1. Syntax Errors: It’s like typos that you do while writing codes, but VBA can help you by point out these errors.
  2. Compile Errors: It comes when you write a code to perform an activity, but that activity is not valid.
  3. Runtime Errors: A RUNTIME error occurs at the time of executing the code. It stops the code and shows you the error dialog box.
  4. Logical Error: It’s not an error but a mistake while writing code and sometimes can give you nuts while finding and correcting them.

Write YOUR‌ First Program in Excel

I have a strong belief that in the initial time when someone is starting programming in Excel, HE/SHE should write more and more codes from scratch.

The more codes you write from scratch, the more you understand how VBA works.

But you need to start with writing simple codes instead of jumping into a complex one.

That’s WHY I don’t want you to think about anything complex right now.

You can even write a macro code to create a pivot table, but right now, I don’t want you to think that far.

Let’s think about an activity that you want to perform in your worksheet, and you can write a code for it.

Use the following steps to write a program in Excel.

  • Go to the Developer Tab and open the Visual Basic Editor from the “Visual Basic” button.
    1-visual-basic-button
  • After that, insert a new module from the “Project Window” (Right-click ➢ Insert ➢ Module).
    2-insert-a-new-module
  • After that, come to the code window and create a macro with the name “Enter Done” (we are creating a SUB procedure), just like I have below.
    3-code-window
  • From here, you need to write a code which we have just discussed above. Hold for second and think like this: You need to specify the cell where you want to insert the value and then the value which you wish to enter.
  • Enter the cell reference, and for this, you need to use RANGE object and specify the cell address in it, like below:
    4-cell-reference-range-object
  • After that, enter a dot, and the moment you add a dot, you’ll have a list of properties that you can define and activities that you can do with the range.
    5-enter-a-dot
  • From here, you need to select the “Value” property and set the text which you want to insert in the cell “A1” and when to do it, your code with look something like below.
    6-select-value
  • Finally, above the line of code, enter the text (‘this code enters the value “Done” in the cell A5). It’s a VBA Comment that you can insert to define the line of code that you have written.
    7-enter-the-text-above-line-code
Sub Enter_Done()
'this code enters the value “Done” in the cell A5
Range("A1").Value = "Done"
End Sub

Let’s understand this…

You can split this code into two different parts.

In the FIRST part, we have specified the cell address by using the RANGE object. And, to refer a cell using a range object you need to wrap the cell address with double quotes (you can also use square brackets).

In the SECOND part, we have specified the value to enter into the cell. What you have done is, you have defined the value property for the cell A5 by using “.Value”. After that, the next thing that you have specified is the value against the value property. Whenever you are defining a value (if it’s text), you need to wrap that value inside double quotation marks.

VBA is one of the Advanced Excel Skills, and if you are getting started with VBA, make sure to check out there (What is VBA and Useful Macro Examples and VBA Codes).