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
We have assigned the string "Ravi Sagar" to the variable myVar.
Displaying the value stored in the Variable
When you print the variable the value stored in the variable will be displayed on the screen.
Things to remember while declaring variables
The name must begin with an alphabetic character and cannot exceed 255 characters or contain any spaces. You should avoid the use of punctuation marks or other unusual characters in the variable name, as many of them are not allowed; however, the underscore character is allowed and works well for separating multiple words contained within a single variable name (for example, First_Name). Avoid using reserved VBA keywords and don’t repeat variable names within the same scope.
NOTE: Use Option Explicit in the general declarations section of a module window to force explicit variable declarations.
Variable Scope
Scope, 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 manipulated. When a variable is out of scope, it is unavailable—essentially invisible to the program.
Data Types
Data types define the kind of value that may be stored within the memory allocated for a variable.
Constants
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
Whenever in your code you use the Constant "PI", its value will be replaced by 3.14159.
circumference = PI* diameter will be translated to circumference = 3.14159* 10.32
Recent comments
21 weeks 3 days ago