0% found this document useful (0 votes)
56 views16 pages

VBA2 Conditional Statement

This document discusses various conditional statements in VBA including if, for, while, do loop, and select statements. It provides examples of how to use relational and logical operators with these statements. It also introduces arrays, data types, and how to declare point data structures in VBA.

Uploaded by

sathishjey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views16 pages

VBA2 Conditional Statement

This document discusses various conditional statements in VBA including if, for, while, do loop, and select statements. It provides examples of how to use relational and logical operators with these statements. It also introduces arrays, data types, and how to declare point data structures in VBA.

Uploaded by

sathishjey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

VBA 2

Conditional Statement

6
Lecture 1

What is conditional statement


Allow
All
decision
d i i
to
t b
be made
d
Type of conditional statement: if, for,
while, do loop and select
Every
y conditional statement is
controlled relational or/and logical
operators
Lecture 6

Operators
Relational Operators
>
<
>=
<=
<>
=

Greater than
Less than
Greater than or equal to
Less than or equal to
Not Equal to
Equal to

Logical Operators
AND, OR
Lecture 6

Example
A = 1,
1 B = 2,
2 C = 3,
3 D=4
A>B
B <> C
A
A
A
A
A
A

=
>
>
=
>
>

1
B
B
1
B
B

FALSE
TRUE

OR B <> C
OR B <> C
OR B = C
AND B <> C
AND B <> C
AND B = C

D=4
B >= 2
T
F
F
T
F
F

OR T
OR T
OR F
AND T
AND T
AND F

Lecture 6

TRUE
TRUE

TRUE
TRUE
FALSE
TRUE
FALSE
FALSE

If then if CONTROL
Basic If statement

If Ans =1 Then
(statement for TRUE)
End If

If then else statement

If Ans1 = 1 Then
(statement
( t t
t for
f TRUE)
Else
(statement for FALSE)
End If
Lecture 6

If then: Example
If End if
If Rad < 0 Then
MsgBox Radius must be positive
Message = Radius of the circle "
Title = "InputBox"
InputBox
Default = 10.0"
Rad = InputBox(Message, Title,Default)
End If

Lecture 6

Nested If Else Statement


If Ans = 0 Then

Else If Ans < 0 Then

End If
Note: use tab to differentiate the level of If
Lecture 6

'For ... Next' CONTROL


For Count
F
C
t = 1 to
t 10

(TRUE until counter > 10)


Next Count
Dim Count, Total As Integer
For Count = 1 to 10
Total = Val(Total) + Val(Count)
Next Count
MsgBox
g
Total number 1 to 10 is &Val(Total)
Lecture 6

'While ... Wend' CONTROL


Counter = 0

counter initialization

While Counter < 10


(VBA statement)
Counter = Counter + 1
Wend
Note: counter must be set, if not the programming will continuing
looping

Lecture 6

Sub main()
Dim Counter, Total As Integer
Counter = 0
Total = 0
While Counter < 11
Total = Total + Counter
Counter = Counter + 1
Wend
g
" Total number 1 to 10 is " & Val(Total)
(
)
MsgBox

End Sub
Lecture 6

10

'Do ... Loop' CONTROL


Score = 0
Do
Score = Score + 1

Loop Until Score = 10


Sub main()
Dim Counter, Total As Integer
Counter = 0
Total = 0
Do
Total = Total + Counter
Counter = Counter + 1
Loop Until Counter = 11
MsgBox " Total number 1 to 10 is " & Val(Total)
Lecture 6
11
End Sub

'Select Case ... End Select'


Select
S
l t C
Case A
Case Is < 0
MsgBox "Number is negative"
Case Is < 2
MsgBox "Number is between 0 and 2"
Case Is < 5
MsgBox "Number is between 2 and 5"
Case Else
MsgBox "Large

number"

End Select

Lecture 6

12

Task: continued
D
Draw the
th user has
h the
th choice
h i the
th
draw the polygon

Lecture 6

13

Array

Indexed group of data treated as a single variable


Array declaration
Dim Point(1 To 50) As Integer
or
Dim Point As Variant
Eg:
Dim aintItems(1 To 50) As Integer
Dim intI As Integer
For intI = 1 To 50
aintItems(intI) = intI
Next intI
varItems = aintItems

Lecture 6

14

Type Data Structure


Point has th
three
ee coordinates:
coo dinates x, y, z. Type
T pe can be used
sed
to point data declaration
Data
D
t Declaration
D l
ti
Type Point
x As Double
y As Double
z As Double
End Type
yp
Point Pt(1 to 10) As Point

Lecture 6

15

Dim swApp As Object


Type Point
x As Double
y As Double
z As Double
End Type
Sub main()
Set swApp = Application.SldWorks
Dim Vertex
e te As
s Point
o t
Vertex.x = 10.5
Vertex.y = 10.5
Vertex.z = 0#
Dim Msg, Style, Title, Response
Msg = Vertex.x
Style = vbOKOnly
Title = "Vertex.x"
R
Response
= M
MsgBox(Msg,
B (M
St
Style,
l
Titl
Title)
)

End Sub
Lecture 6

16

You might also like