In the previous article, we learnt about the VBA elements which may constitute together to make a subroutine. In this article let’s look these elements in details.
Table of Contents
ToggleComments
A comment is descriptive text in VBA Code. It’s a good idea to use comments to describe what you’re doing. A comment is indicated by an apostrophe ( ‘ ). VBA ignores any text that follows an apostrophe—except when the apostrophe is contained within quotation marks ( ” ).
For example, the following statement doesn’t contain a comment, even though it has an apostrophe:
Message = “Let’s Walk”
The following are a few general tips on making the best use of comments:
■ Use comments to describe the purpose of procedure.
■ Use comments to describe changes you make.
■ Use comments to indicate you’re using functions or constructs in an unusual manner.
■ Use comments to describe the purpose of variables.
■ Write comments while writing code rather than afterward.
Assignment Statements
An assignment statement is a VBA instruction that evaluates an expression and assigns the result to a variable. In other words, the result of a VBA expression can be assigned to a variable. VBA uses the equal sign (=) as its assignment operator. The following are examples of
assignment statements:
x = 1 This means 1 is assigned to X. Now value of x is 1.
a = a + 1 This means value of a + 1 is assigned to a. Suppose value of a is given as 5. Now value of a after execution of this statement will become 6.
Variables
You can think of a variable as a name of box which can hold values of different types of data. It can hold number, text, dates, Boolean values, and so on. We will learn data types in details afterwards.
You assign a value to a variable by using the equal sign operator (as explained earlier) The names of the variables should be as descriptive as possible.
VBA has a few rules regarding variable names.
- You can use alphabetic characters, numbers, and some punctuation characters, but the first character must be alphabetic.
- VBA variables are case insensitive.
- You can’t use spaces or periods.
- You can’t use characters (#, $, %, &, or !) in a variable name. Underscore is allowed ( _ ).
- Variable names can be as long as 254 characters.
The MsgBox function
The MsgBox function is one of the most useful VBA functions. This function can be used as a dialog box between you and VBA. The MsgBox function not only returns
a value but also displays a dialog box to which the user can respond. The value returned by the MsgBox function represents the user’s response to the dialog box. You can use the MsgBox function even when
you have no interest in the user’s response but want to take advantage of the message display.
The syntax of the MsgBox function has five arguments.
MsgBox(prompt[, buttons][, title][, helpfile, context])
prompt: The message displayed in the pop-up display.
buttons: A value that specifies which buttons and which icons, if any, appear in the message box. for example, vbYesNo.
title: The text that appears in the message box’s title bar.
helpfile: The name of the Help file associated with the message box.
context: The context ID of the Help topic.
You can also assign the value returned to a variable.