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 Sub and Function procedures

In this recipe, we will be creating the two most common types of procedures; Sub and Function procedures.

A Sub procedure is, in essence, a series of VBA statements, with the purpose of performing an action within Excel. The simplest Sub procedures can be as short as a single line of code (between the Sub and End Sub statements), while complex Sub procedures can become quite long. It is, however, advisable to divide huge Sub procedures into a series of shorter Sub procedures.

Another procedure often used in VBA is the Function procedure. The purpose and use of this type of procedure differs from the Sub procedure in the sense that it performs a calculation and returns a single value.

It can happen that you want to do a complex calculation, like a nested If statement, or a nested And/Or statement. By incorporating these functions into a User-Defined Function (UDF), you can simplify the process considerably.

Function procedures can also...