Académique Documents
Professionnel Documents
Culture Documents
Variables are used in coding to hold values, and enable various functions
and calculations. When you create one, you will have to choose from a list
of available data types. The basic syntax for doing this is:
Dim x as _______
Where x is the name of your variable (you can use any name that you
want), and underscore is replaced by the desired data type.
Types:
Boolean: These variables will be able to have values of True or False.
They cannot contain arbitrary numeric or text values.
Date: Holds a date between January 1st, 0100 and December 31st, 9999.
This creates an obvious Y10K issue if youre planning using the same
Excel file for the next 7,985 years.
String: Holds a text string. The size limitation is not an issue. They can
hold 2 billion characters.
Integers: These represent numbers without decimal places. They can
have positive or negative values. These variables take up a limited
amount of data and are subsequently limited to values between 32,768
and -32,768.
Decimal: These variables take up more space and allow for more
accuracy. Because of this they can be slower, too. They can store numbers
that contain approximately 28 number places.
Assigning Values to Variables
If all you want to do is give your variable a fixed value, then you can use
the following syntax:
X=5
Using worksheet cells value:
X = Worksheets(name of worksheet).Cells(2,B).value
Recording a Macro:
In the Background Color Panel, select the Ligth Yellow color box. This action will set the
background of the current cell (A1) in light yellow color.
Now run the Recorded Macro, On any worksheet, select from D3 to E6 and Run the
recorded macro by select Tools...Macro.
Cells D3 to E6 now have light yellow background color
The following sub procedure (or sub routine) print the current date and time on cell C1:
Sub ShowTime()
Range("C1") = Now()
End Sub
The following function sum up two numbers:
Function sumNo(x, y)
sumNo = x + y
End Function
Procedures in Visual Basic can have either private or public scope.
A procedure with private scope is only accessible to the other procedures in the
same module
Sub procedures:
MsgBox "Welcome!"
End Sub
Function Procedures
Example 1
===================== Function
=====================================
Function partNames() As String
=========================Sub
Procedure======================
Sub fullName()
ActiveSheet.Range("A1") = partNames
End Sub
Workbook module:
Sheet Modules:
A sheet module has the same name as the worksheet's code name
with which it is associated viz. Sheet1, Sheet2
UserForm Modules:
Class Modules:
Class Modules are used to create new objects and you can create
your own custom events in a class.
Calling a Function
Function CallMe() As String
Dim A As String
A = "Hello World"
CallMe = A
End Function
Sub Exercise()
CallMe
End Sub
Arguments
Sub ProcedureName(Argument)
End Sub
String Functions:
Len Function
=LEN("123")
Result: 3
LCase Function
Syntax: LCase ( string )
Example:
Print Lcase("Good Morning")
'Output --> good morning
UCase Function
Syntax: UCase(string)
Example:
Print Ucase("Good Morning")
'Output --> GOOD MORNING
Left Function
Syntax: Left(string, length)
Example:
Print Left("Good Morning",4)
'Output --> Good
Right Function
Looping Concept:
For Next:
Syntax:
For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next
Example1:
Dim i As Integer
For i = 1 To 5
Cells(i, 1).Value = 100
Next i
Result in excel:
Example2:
Dim i As Integer, j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Result in excel:
Do While Loop:
Syntax:
Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]
Loop
OR
Do
[statements]
[Exit Do]
[statements]
Loop [{While | Until} condition]
Example:
Dim i As Integer
i=1
Do While i < 6
Cells(i, 1).Value = 20
i=i+1
Loop
Result in excel:
If...Then...Else Statement
Syntax:
Syntax:
score = Range("A1").Value
While...Wend statement:
Syntax:
While condition
Version [statements]
Wend
Example:
Counter =1
While Counter < 4
Total =Inputbox("Please enter the total marks in numbers")
If Total < 50 Then
MsgBox "Fail"
ElseIf Total >=50 and Total <=60 then
Msgbox "Second Class"
ElseIf Total >60 and Total <80 then
Goto statement:
On Error GoTo Oops
'[more code goes here]
Exit Sub
Oops:
MsgBox "An error occurred"