Word Document Properties


Using Option Explicit to Reduce Errors



Download 8,82 Mb.
Pdf ko'rish
bet66/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   62   63   64   65   66   67   68   69   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Using Option Explicit to Reduce Errors
‘ Tell VBA to ensure we define variables.
Option Explicit
‘ This Sub will fail because it doesn’t
‘ define the variable.
Public Sub OptionCheck()
MyVar = “Hello”
MsgBox MyVar
End Sub
‘ This Sub will succeed.
Public Sub OptionCheck2()
‘ Define the variable.
Dim MyVar As String
‘ Add a value to the variable.
MyVar = “Hello”
‘ Display a message box.
MsgBox MyVar
End Sub
In both cases, the 
Sub
defines a value for a variable named 
MyVar
. The
OptionCheck Sub
fails because it doesn’t define the variable before it uses
it. VBA doesn’t know anything about 
MyVar
, so it can’t use it. Look at the
second 
Sub

OptionCheck2
. This 
Sub
works because it defines 
MyVar
first
and then assigns a value to it. Although using the 
Option Explicit
state-
ment might seem like a lot of work, it really does save time spent debugging
typos in your program.
Taking the Lego Approach
It’s easy to make writing a program harder than it needs to be by getting caught
up in the abstract nature of code. The biggest mistake that you can make is to
write one long program that goes on for pages and pages that no one can
understand — not even you. Code that looks more like a copy of
War and
Peace
rather than a simple set of instructions is usually called 
spaghetti code.
The Lego approach
to writing code is one in which the programmer breaks up
the program into easily understood modules and writes just one module at a
time. Using this approach helps you write code that is easy to modify later
and that most people can understand with ease. The Lego approach also
makes it easy for you to move pieces of code when writing another program.
66

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   ...   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