Book Image

VBA Automation for Excel 2019 Cookbook

By : Mike Van Niekerk
Book Image

VBA Automation for Excel 2019 Cookbook

By: Mike Van Niekerk

Overview of this book

Visual Basic for Applications (VBA) is a programming language developed by Microsoft to automate tasks in MS Office applications. This book will help you to focus on the essential aspects of your role by automating mundane tasks in Excel and other Office applications. With comprehensive coverage of VBA delivered in the form of practice problems and bite-sized recipes, this book will help you to hit the ground running. Unlike most books that assume prior programming experience, this book starts with the fundamentals and gradually progresses to solving bigger problems. You’ll start by becoming familiar with VBA so that you can start recording macros right away. With this foundation in place, you’ll advance to using the full capabilities of the language as you apply loops, functions, and custom dialog boxes to design your own automation programs. You'll also get to grips with embedded macros and other advanced tools to enhance productivity and explore topics relating to app performance and security. Throughout this VBA book, you’ll cover multiple practice projects in Excel, Word, and PowerPoint while exploring tips and best practices to hone your skills. By the end of this book, you’ll have developed the skills you need to use VBA to create your own programs that control MS Office applications.
Table of Contents (20 chapters)

Creating a basic procedure in the code window of the Editor

In this recipe, we will create a basic procedure in the code window of the Editor. We're at the point where everything is coming together now; you know the different components, and you know how to create a module. All that's left is to create code – a simple Sub procedure – in the newly created module.

Getting ready

Make sure that you've created a module in the VBA Editor. The module should be visible as an object in the Project window, and the title bar of the code window should display the name Module1 (Code). If you cannot see the title bar, click on the restore window button to make the window smaller.

How to do it…

Let's go through the steps for this recipe:

  1. In the code window, type the following code:
    Sub MyName()
        MsgBox "My name is Yourname" (Replace Yourname _
        with your first name.)
    End Sub
  2. Press Alt + F11 to switch to Excel.
  3. Click on any cell of the active sheet. Go to Developer | Code, and click on the Macros icon. The Macro dialog box appears:
Figure 2.16 – The Macro dialog box

Figure 2.16 – The Macro dialog box

The only available macro is the one that you've just created; MyName. Click on the Run button.

A message box will appear with the words My name Mike, as shown:

Figure 2.17 – Message box from a basic procedure

Figure 2.17 – Message box from a basic procedure

A much shorter way to execute the code is to press F5 while in the VBA Editor. You will automatically switch to Excel, where the message box will be displayed.

How it works…

Entering code in the VBA Editor is relatively simple, on the condition that you have a basic understanding of VBA coding. Looking at a few code samples online before going solo is advisable, and will save you many hours of frustration.

As a bonus, the Editor will make adjustments to the text you enter. The first example of this is when you type the Sub statement and the name of your procedure. When you press Enter, the Editor automatically inserts the End Sub statement. As we progress, you will see how spaces are inserted, and how capitalization and text colors are changed.

Once your code is error-free, the code can be run. In this case, the outcome is a message box, displaying the words My name Mike.