book

Tags:

Numeric expressions are what I normally think of when I use the generic term "expression." Whether it's calculating gross margin, figuring out commissions, or determining the monthly payment on a loan, many expressions perform some kind of number crunching. You saw VBA's arithmetic operators earlier in this chapter. This section adds to that by giving you a quick look at VBA's built-in math and financial functions.

VBA's Math Functions

Tags:

You'll often use simple expressions that contain just two values and a single operator. In practice, however, many expressions you use will have a number of values and operators. In these more complex expressions, the order in which the calculations are performed becomes crucial. For example, consider the expression 3+5^2. If you calculate from left to right, the answer you get is 64 (3+5 equals 8 and 8^2 equals 64). However, if you perform the exponentiation first and then the addition, the result is 28 (5^2 equals 25 and 3+25 equals 28).

Tags:

The Assignment Operator
We have already seen the first of VBA's operators: the assignment operator, which is just the equals sign (=). It is used to assign the result of an expression to a variable.
Always keep in mind that VBA always derives the result of the right side of the equation (that is, the expression) before it modifies the value of the left side of the equation.

For example, consider the following code fragment:

currentYear = 2004
currentYear = currentYear + 1
Tags:

VBA Expressions

Tags:

A comment is descriptive text embedded within your code. The text of a comment is completely ignored by VBA. It’s a good idea to use comments liberally to describe what you’re doing.

You can use a complete line for your comment, or you can insert a comment after an instruction on the same line. A comment is indicated by an apostrophe. VBA ignores any text that follows an apostrophe—except when the apostrophe is contained within quotation marks—up until the end of the line. For example, the following statement does not contain a comment, even though it has an apostrophe:

Tags:

Arrays

Tags:

Variables
Variables are basically used in a Code for Temporary storage.

Declaring a Variable
To declare a variable is to tell the computer to reserve space in memory for later use. To declare a variable use a Dim (short for Dimension) statement.

dim myVar as String

Here String is the Data Type of the variable, which tell what kind of data to store in the variable. Here we have used String which means that myVar will be used to store characters.

Assigning value to a Variable

myVar = "Ravi Sagar"
Tags:

We will briefly discuss the basic Programming constructs used in the VBA Programming Programming. Any VBA code is build up using these constructs. You can say that these are the building blocks of the Macro.

Tags:

A procedure holds a group of VBA statements that accomplishes a desired Task. A procedure is a series of VBA statements that resides in a VBA module, which you access in the VBE. A module can hold any number of procedures. You have a number of ways to call, or execute, procedures. A procedure is executed from beginning to end (but it can also be ended prematurely).

Tags:

Yeah it is compulsory to have a Hello World! Program in all the Programming Books. So here it is.

1. Create a new Module as explained earlier.
2. Type sub Hello_World and press Enter.
3. Press Tab, type msgbox "Hello VBA World!", and press Enter.
4. Press CTRL + S to save the Module. After completing these steps your Macro would appear as below.

Sub Hello_World()
 MsgBox "Hello VBA World!" 
End Sub

Run this Macro and be amazed.

1. Go back to the Excel Sheet
2. Press ALT + F8