Word Document Properties


Listing 9-1 Creating and Using an Array for String Data



Download 8,82 Mb.
Pdf ko'rish
bet174/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   170   171   172   173   174   175   176   177   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 9-1
Creating and Using an Array for String Data
‘ Tell VBA to start all arrays at 0.
Option Base 0
Public Sub SingleDimension()
‘ Define an output string.
Dim Output As String
‘ Define a variant to hold individual strings.
Dim IndividualString As Variant
‘ Define the array of strings.
Dim StringArray(5) As String
‘ Fill each array element with information.
StringArray(0) = “This”
StringArray(1) = “Is”
StringArray(2) = “An”
StringArray(3) = “Array”
206
Part III: Expanding Your VBA Horizons 
15_046500 ch09.qxp 12/5/06 5:36 PM Page 206


StringArray(4) = “Of”
StringArray(5) = “Strings”
‘ Use the For Each...Next statement to get each array
‘ element and place it in a string.
For Each IndividualString In StringArray
‘ Create a single output string with the array
‘ array elements.
Output = Output + IndividualString + “ “
Next
‘ Display the result.
MsgBox Trim(Output), _
vbInformation Or vbOKOnly, _
“Array Content”
End Sub
Notice that the code begins with an 
Option Base 0
statement. This state-
ment tells VBA whether you want to start counting array elements at 0 or 1.
The default setting is 0. Most programming languages use 0 as the starting
point, which is why Microsoft made 0 the default for VBA. However, older ver-
sions of Visual Basic (including VBA) use 1 as the starting point. When you
want to ensure that your program works in every environment, include the
Option Base
statement.
The code for 
SingleDimension
begins with some variable declarations.
Notice the 
StringArray
declaration. When you want to create an array, you
follow the variable name with a pair of parentheses that contains the number
of elements. You can also create an empty array by leaving out the number,
but then you need to use the 
ReDim
statement to set the number of elements
later. See the upcoming “Understanding the array types” section for details.
Because the array begins at 0 and not at 1, you can actually store six items in
an array that is defined as having five elements. The number that you include
in the declaration is always the top element number of the array and not the
actual number of elements.
The code that follows the array declaration fills each of these elements with a
string. Notice the use of numbers in the statement. This number is an 
index
.
You use the index to access individual members of the array, just as you use
letters or numbers to access individual apartments in an apartment complex.
The statement 
StringArray(1) = “Is”
places the word 
Is
in the second
array element by using an index of 1. You can always access an individual ele-
ment by using its index.
207

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   170   171   172   173   174   175   176   177   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©www.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