Word Document Properties


Listing 5-3 Using the If...Then...ElseIf Statement for Comparisons



Download 8,82 Mb.
Pdf ko'rish
bet109/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   105   106   107   108   109   110   111   112   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 5-3
Using the If...Then...ElseIf Statement for Comparisons
Public Sub CompareNumbers2()
‘ Create variables to hold the two numbers.
Dim Input1 As Double
Dim Input2 As Double
‘ Create an output string.
Dim Output As String
‘ Fill the variables with input from the worksheet.
(continued)
117
Chapter 5: Creating Structured Programs
10_046500 ch05.qxp 12/5/06 5:35 PM Page 117


Listing 5-3 
(continued)
Input1 = Sheet1.Cells(3, 2)
Input2 = Sheet1.Cells(4, 2)
‘ Determine if the first number is greater than
‘ the second number.
If Input1 > Input2 Then
‘ Tell the user the first number is greater.
Output = “First Number is greater than “ + _
“Second Number.”
‘ Determine if they are equal.
ElseIf Input1 = Input2 Then
‘ Tell the user they are equal.
Output = “The values are equal.”
Else
‘ The first number is less than the second.
Output = “First Number is less than Second
Number.”
End If
‘ Place the output on the worksheet.
Sheet1.Cells(6, 2) = Output
End Sub
The first thing that you should notice is that this code avoids nesting, which
makes it easier to read. VBA will see the first 
If...Then
statement and
determine whether 
Input1
is greater than 
Input2
. If the first statement is
true, VBA sets the value of 
Output
and leaves the structure. However, if the
first statement is false, VBA checks the 
ElseIf
statement. Finally, if the first
two statements are false, VBA executes the statements within the 
Else
part
of the structure. You can use as many 
ElseIf
statements as you need to
check every possible condition in your code.
This code is almost as efficient as the first version — it requires just one deci-
sion for greater than, two for equality, and three for less than. The ease of
reading might become more important to you when the complexity of your
programs increases, so this is a perfectly acceptable way to write your code.
In short, this example demonstrates that there’s usually more than one way
to write a program, many of which work fine; the choice of which method to
use depends on your personal tastes.
118

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   105   106   107   108   109   110   111   112   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish