CHAPTER 3VBA Programming Fundamentals
VBA Language Elements: An Overview
If you've used other programming languages, much of the information in this chapter may sound familiar. However, VBA has a few unique wrinkles, so even experienced programmers may find some new information.
This chapter explores the VBA language elements, which are the keywords and control structures that you use to write VBA routines.
To get the ball rolling, take a look at the following VBA Sub procedure. This simple procedure, which is stored in a VBA module, calculates the sum of the first 100 positive integers. When the code finishes executing, the procedure displays a message with the result.
Sub VBA_Demo()' This is a simple VBA ExampleDim Total As Long, i As LongTotal = 0For i = 1 To 100Total = Total + iNext iMsgBox TotalEnd Sub
This procedure uses some common VBA language elements, including the following:
- A comment (the line that begins with an apostrophe)
- A variable declaration statement (the line that begins with
Dim) - Two variables (
Totalandi) - Two assignment statements
(Total = 0andTotal = Total + i) - A looping structure (
For-Next) - A VBA function (
MsgBox)
You will explore all of these language elements in subsequent sections of this chapter. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access