Word Document Properties


Listing 5-1 Using an If...Then Statement for Decisions



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

Listing 5-1
Using an If...Then Statement for Decisions
Public Sub IfThenTest()
‘ Create a variable for the selected text.
Dim TestText As String
‘ Get the current selection.
TestText = ActiveWindow.Selection.Text
‘ Test the selection for “Hello.”
If TestText = “Hello” Then
‘ Modify the selected text to show it’s correct.
TestText = “Correct!” + vbCrLf + “Hello”
End If
‘ Test the selection for end of line.
If TestText = Chr(13) Then
‘ Modify the selected text to show the control
(continued)
113
Chapter 5: Creating Structured Programs
10_046500 ch05.qxp 12/5/06 5:35 PM Page 113


Listing 5-1 
(continued)
‘ character.
TestText = “End of line selected!”
End If
‘ Test the selection for a space.
If TestText = Chr(32) Then
‘ Modify the selected text to show the space.
TestText = “Space selected!”
End If
‘ Display the selected text.
MsgBox TestText
End Sub
The code begins by creating a variable to hold the selected text. It then
shows how to use a new object, 
ActiveWindow
. The 
ActiveWindow
object
is useful for a number of tasks, but in this case you use it to get hold of the
selected text, if any. If you don’t have any text selected, the resulting message
box may simply display an OK button without any additional information.
The first 
If...Then
structure determines whether you select the word
Hello.
If so, the statement 
TestText = “Correct!” + vbCrLf +
“Hello”
executes. Otherwise, VBA ignores the statement and moves on to
the next test.
The second 
If...Then
structure determines whether the cursor is at the
end of the line. This test tells you two things about selections. First, VBA
always sees at least one character selected. If you haven’t selected anything,
ActiveWindow.Selection.Text
returns the next character in line.
Second, the 
vbCrLf
constant does have two control characters in it. The
code tests for 
Chr(13)
or a carriage return because that’s always the first
character in the 
vbCrLf
sequence. If you set the cursor at the end of the line,
VBA modifies 
TestText
as shown in the code.
The third 
If...Then
structure checks for a space. Although it isn’t always
essential to check for spaces, in this case viewers will see a blank dialog box
when they place the cursor immediately before a space in the line of text.
Making this additional check reduces the risk that the user will see some-
thing unexpected. You should always try to include checks such as this one
in your code. Unfortunately, you won’t always find all of these difficult sce-
narios immediately, which is why you should ask other people to check your
applications for potential problems.
114

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   102   103   104   105   106   107   108   109   ...   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