Vous êtes sur la page 1sur 24

Condition Statements & Loop

Condition Statement
Condition: If we have multiple situation with multiple result then
we have to use condition statement (IF)

There are different condition statements:

IF EndIF

IF Else EndIF

IF ElseIF . Else . EndIF

Nested Ifs

Where Learning is sharing... 2


IF .. End IF Condition Statement
Syntax: Syntax:

if condition then if condition then

_______________
_______________
else
_______________
_______________
End if
End if
E.g. if a>100 then
E.g. if a>100 then
msgbox a msgbox a
End if else

msgbox Other

End if
IF ELSEIF .. Condition Statement
Syntax: E.g.

if condition then If a<100 then

Msgbox a
_______________
Elseif a<200 then
elseif condition then
Msgbox <200
_______________
Else
else
Msgbox >200
_______________ End if
End if
Nested IF Condition Statement
Syntax: E.g.

if condition then If a<100 then

Msgbox a
_______________
Else
else
if a<200 then
if condition then
Msgbox <200
_______________
Else
else Msgbox >200
_______________ End if
end if End If

End if
Loop
Loop: If we want to carry out a set of actions to repeat a
number of times then use Loop

There are different condition statements:

For Loop

Do While Loop

Do Loop While

Do Until Loop

Do Loop Until

For Each Next

With End With

Where Learning is sharing... 6


For Loop
Syntax: For VarName= Start to End [Step value]

______________________

Next [VarName]
For = Keyword , VarName = any user defined variable name

Start = starting number in the loop

End = ending number in the loop

Step = keyword and step value = value by which the loop to


be inceremented. By default it is 1 and it is optional

Next = Keyword , VarName = Optional

Where Learning is sharing... 7


For Loop Examples
1) For I = 1 to 10

Msgbox I

Next

2) For I =2 to 200 step 2

Msgbox I

Next I

3) For I =1000 to 1 step -5

Msgbox No: & I

next

Where Learning is sharing... 8


Do While Loop
In Do While Loop, condition is checked before iteration. So
if condition fails then loop exit else loop will be iterated

Syntax: Do While condition

______________________

Loop
Do While = Keyword

Condition = logical condition

Loop = Keyword which indicates end of the loop

Note: Unlike For .. Next loop, we do have to manually


increment the loop
Where Learning is sharing... 9
Do While Loop Examples
1) Do While a<100

Msgbox a

a=a+1

loop

2) Do While a>0

Msgbox a

a=a-1

loop

Where Learning is sharing... 10


Do Loop While
In Do Loop While, condition is checked after 1st iteration. So
if condition fails then also loop will be iterated for one time

Syntax: Do

______________________

Loop While condition


Do = Keyword indicates start of the loop

Loop While = Keyword which indicates end of the loop

Condition = logical condition

Note: Unlike For .. Next loop, we do have to manually


increment the loop
Where Learning is sharing... 11
Do Loop While Examples
1) Do

Msgbox a

a=a+1

loop While a<100

2) Do

Msgbox a

a=a-1

loop While a>0

Where Learning is sharing... 12


Do Until Loop
In Do Until Loop, condition is checked before iteration. So if
condition fails then loop exit else loop will be iterated

Syntax: Do Until condition

______________________

Loop
Do Until = Keyword

Condition = logical condition

Loop = Keyword which indicates end of the loop

Note: Unlike For .. Next loop, we do have to manually


increment the loop
Where Learning is sharing... 13
Do Unil Loop Examples
1) Do Until a<100

Msgbox a

a=a+1

loop

2) Do Until a>0

Msgbox a

a=a-1

loop

Where Learning is sharing... 14


Do Loop Until
In Do Loop Until, condition is checked after 1st iteration. So if
condition fails then also loop will be iterated for one time

Syntax: Do

______________________

Loop Until condition


Do = Keyword indicates start of the loop

Loop Until = Keyword which indicates end of the loop

Condition = logical condition

Note: Unlike For .. Next loop, we do have to manually


increment the loop
Where Learning is sharing... 15
Do Loop Until Examples
1) Do

Msgbox a

a=a+1

loop Until a<100

2) Do

Msgbox a

a=a-1

loop Until a>0

Where Learning is sharing... 16


For Each Next Loop
Syntax: For Each VarName in Collection

______________________

Next [VarName]
For = Keyword

VarName = any user defined variable name or variable of


object type

In = Keyword

Collection = group of variable type

Where Learning is sharing... 17


For Each Next Loop Examples
1) For each sht in worksheets

Msgbox sht.name

Next sht

2) For each book in workbooks

Msgbox book.name

Next book

Where Learning is sharing... 18


With End With
Syntax: With objectType

.property1

.property2

__________

End With
With = keyword

ObjectType = Any object

.property = setting property of mentioned objecttype

End With = indicates end of loop


Where Learning is sharing... 19
With End With Examples
1) With Range(A1:B7)

.value =20

.font.bold =true

.color=65536

.borders.linestyle = xlContinous

End With

2) With Activesheet

.name= sht

End with

Where Learning is sharing... 20


Goto Statement
GoTo: GoTo statement transfers program execution to a new
instruction which is preceded by a label

Thus, Goto statement is used to jump the code execution from


one line to other line

Syntax: GoTo lablename

Goto = Keyword which tells VBA compiler to jump the code


execution

LabelName = VBA compiler jumps the code execution to


LabelName. Labelname is any string

Note A colon (:) is used to describe functionality of the label

Where Learning is sharing... 21


Goto Statement
E.g. Sub test()

If a<10 then

Goto disp

Else

Msgbox a>=10

End if

Exit sub

Disp:

msgbox Value of a is less than 10

End Sub

Where Learning is sharing... 22


Select Statement
Select Statement: When we want to select a single option out
of we multiple choices, then will use Select statement

Syntax: Select case casename

Case case1

__________________

Case else

__________________

End select

Casename can be number or string or expression

Where Learning is sharing... 23


Select Statement Example
Sub test() Sub test1()
Q = inputbox(QTY =) C= inputbox(color=)
Select Case Q Select Case C
Case 1 Case a or A
Msgbox QTY = 1 Msgbox ALL
Case 2 Case b or B
Msgbox QTY = 2 Msgbox Blue
Case 3 to 25 Case R or r
Msgbox 3 to 25 Msgbox Red
Case else Case else
Msgbox Other Msgbox Other
End select End select
End sub End sub

Where Learning is sharing... 24

Vous aimerez peut-être aussi