Visual Basics for Application

17
VISUAL BASIC FOR APPLICATIONS WITH EXCEL

Transcript of Visual Basics for Application

Page 1: Visual Basics for Application

VISUAL BASIC FORAPPLICATIONS WITH

EXCEL

Page 2: Visual Basics for Application

Variables and data types

Constants

Simple input and output

String functions

Page 3: Visual Basics for Application

Declaring Variables :

To declare a variable, use a Dim (short for Dimension) statement.

Dim myVar As Integer

The name of the variable is myVar.

Note :- The name must begin with an alphabetic character and cannot exceed 255

characters or contain any spaces.

Use Option Explicit in the general declarations section of a module window to force explicit variable declarations

Page 4: Visual Basics for Application

Object and Standard Modules

Modules refer to a related set of declarations and procedures.

Each module will have a separate window in the VBA IDE and, depending on the origination of the module, it will have different behavior with regard to variable declarations.

This module will automatically contain all event procedures associated with the worksheet Sheet1, and any ActiveX controls added to this worksheet.

A standard module must be added to the project via the Insert menu of the VBA IDE.

Page 5: Visual Basics for Application

The object modulefor an Excelworksheet.

Page 6: Visual Basics for Application

Inserting a standard module.

Page 7: Visual Basics for Application

Variable ScopeScope, in the context of variables, refers to the time when a variable is visible or available to the program. When a variable is in its scope, it can be accessed and/or anipulated. When a variable is out of scope, it is unavailable—essentially invisible to the program.

Private Sub Worksheet Activate()

Static myVar4 As Integer

myVar4 = myVar4 + 1

End Sub

In this procedure the variable myVar4 will increment its value by one with each call to the procedure. If you replace the Static keyword with Dim, myVar4 will never exceed a value of 1. Integer variables are initialized to a value of 0 at declaration.

Page 8: Visual Basics for Application

Data Types

Data types define the kind of value that may be stored within the memory allocated for a variable.

As with spreadsheet cells, there are numerous data types

Page 9: Visual Basics for Application

Data Type Storage Size Range

Boolean 2 bytes True or False

Integer 2 bytes -32,768 to 32,767

Long 4 bytes - 2,147,483,648 to 2,147,483,647

Single (floating-point) 4 bytes - 3.402823E38 to 1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive

values

Currency 8-byte 922,337,203,685,477.5808 to

922,337,203,685,477.5807

Object 4 bytes Any Object reference

String (variable-length 10 bytes + string length 0 to approximately 2 billion

Page 10: Visual Basics for Application

Integer, long, single, and double.

A variable declared as an integer or long data type can hold whole numbers or non-fractional values within the specified ranges.

Common Mathematical Operators Used In VBA

Operation Operator

Addition +

Subtraction -

Multiplication *

Division /

Exponential ^

Page 11: Visual Basics for Application

Basically, any mathematical operation that can be performed on a number can be performed on a numerical variable. The following are a few examples:

Dim num1 As Integer

Dim num2 As Integer

Dim answer As Integer

num1 = 10

num2 = 5

answer = num1 + num2 ' answer Holds 15

answer = num1 num2 ' answer Holds 5

answer = num1 * num2 ' answer Holds 50

answer = num1 / num2 ' answer Holds 2

answer = num1 ^ 2 ' answer Holds 100

answer = 2 ^ num2 ' answer Holds 32

Page 12: Visual Basics for Application

Variables with string data types are used to hold characters as text. The characters can be numbers, letters, or special symbols (for example, punctuation marks).

Basically, just about anything you can type on your keyboard can be held within a string variable. To declare a variable with the string data type, use the String keyword. To initialize a string variable, place the string value within double quotation marks.

Dim myText As String

myText = “VBA is fun”

Dim myString As String * 8

myString = "ABCDEFGHIJKL“

Page 13: Visual Basics for Application

Variant data types are analogous to the General category in the number format of a spreadsheet cell in the Excel application.

Variables are declared as variants by using the keyword Variant, or by not specifying a data type.

Dim myVar

Dim myVar2 As Variant

Variant type variables can hold any type of data except a fixed length string

Dim myVar As Integer

myVar = 10

myVar = "Testing"

Page 14: Visual Basics for Application

The example above will generate a type mismatch error because an attempt is made to enter

the string "Testing" into an integer variable; however, if you change the variable myVar to a

variant, the code will execute and myVar will hold the string value "Testing" when all is complete.

The following code will run without error.

Dim myVar

myVar = 10

myVar = "Testing“

Boolean Data Types:

The Boolean data type holds the value true or false.

Declare and initialize a Boolean variable as follows:

Page 15: Visual Basics for Application

Dim rollDice As Boolean

rollDice = False

Date Data Types

Variables of type date are actually stored as floating-point numbers with the integer portion representing a date between 1 January 100 and 31 December 9999, and the decimal portion

representing a time between 0:00:00 and 23:59:59. The date data type is mostly a convenience

when you need to work with dates or times. Declare and initialize a Date variable as follows:

Dim currentDate As Date

currentDate = Now

A handful of VBA functions use variables of type date that add to this convenience. You will

see a couple of examples of date functions in the chapter project.

Page 16: Visual Basics for Application

Constants allow you to assign a meaningful name to a number or string that will make your code easier to read

Const PI = 3.14159

Dim circumference As Single

Dim diameter As Single

diameter =10.32

circumference = PI* diameter

Page 17: Visual Basics for Application

Function Name Returns

Str() A string representation of a number

Val() A numerical representation of a string

Trim() A string with leading and trailing spaces removed

Left() A portion of a string beginning from the left side

Right() A portion of a string beginning from the right side

Mid() Any portion of a string