1 00:00:01,150 --> 00:00:06,250 In this video, we're going to be talking about array's and how we might use them in visual basic. 2 00:00:06,700 --> 00:00:09,610 And so this begs the question, what is an array? 3 00:00:10,300 --> 00:00:16,570 An array is a specific type of variable in VBA that can store multiple values instead of a single value. 4 00:00:16,960 --> 00:00:23,950 So whereas a normal variable in VBA can only store one value at a time, your array type variable can 5 00:00:23,950 --> 00:00:27,970 store an entire list of values so long as they are the same data type. 6 00:00:28,450 --> 00:00:34,630 So for example, if we had a list of student names John James, Hannah, Connor and Christina, we would 7 00:00:34,630 --> 00:00:40,480 have to create five separate variables if we wanted to store each of those values in its own normal 8 00:00:40,480 --> 00:00:41,710 VBA variable. 9 00:00:42,100 --> 00:00:47,500 If we created an array variable, on the other hand, we could store all five of those names at once 10 00:00:47,530 --> 00:00:50,140 and only require one variable declaration. 11 00:00:50,590 --> 00:00:53,410 So this kind of begs the question, how do arrays work? 12 00:00:54,040 --> 00:00:57,520 So arrays are going to first take your list of values that you're giving. 13 00:00:57,910 --> 00:01:03,160 Turn them into a little less like it's shown on the right and then turn each of those items into an 14 00:01:03,190 --> 00:01:05,230 individual item itself. 15 00:01:05,530 --> 00:01:11,890 So instead of having five separate variables, storing five separate values, we can set up an array 16 00:01:11,890 --> 00:01:14,230 ahead of time to expect five values. 17 00:01:14,560 --> 00:01:16,920 It will create five separate little item boxes. 18 00:01:16,930 --> 00:01:22,300 And then as we assign those item boxes values, each of those items gets assigned its own number. 19 00:01:22,630 --> 00:01:29,650 So in this case, that array on the bottom has green boxes for John, James, Hannah, Connor and Christina. 20 00:01:30,100 --> 00:01:36,700 All five of those names get their own box, their own value, but they all exist within the same array. 21 00:01:37,030 --> 00:01:42,430 They're separated by a numbering system, an indexing system that starts with zero. 22 00:01:42,760 --> 00:01:48,070 And I might not seem intuitive at first, but starting with zero for indexing is pretty standard for 23 00:01:48,070 --> 00:01:49,420 all programming languages. 24 00:01:49,780 --> 00:01:55,900 So instead of counting from one and going up to five four lists of names, the system will start from 25 00:01:55,900 --> 00:01:57,390 zero and go up to four. 26 00:01:57,750 --> 00:02:02,590 And we'll see examples of this when we actually talk about using arrays in visual basic. 27 00:02:03,010 --> 00:02:05,200 You can see here that we have a single array. 28 00:02:05,500 --> 00:02:10,720 It's a single variable, but we're storing all five items separately within our array. 29 00:02:12,120 --> 00:02:16,050 And that's why we like to use arrays, arrays can significantly simplify the code. 30 00:02:16,380 --> 00:02:20,250 Because you can reduce the number of variables that you have to set up a number of variables that you 31 00:02:20,250 --> 00:02:23,520 have to actually go in and store values to and retrieve. 32 00:02:23,520 --> 00:02:28,800 Values from arrays are also a lot more flexible than a regular variable type. 33 00:02:28,830 --> 00:02:31,710 They can continue to grow if we have a longer list. 34 00:02:31,740 --> 00:02:34,020 We can continue to add items onto that list. 35 00:02:34,440 --> 00:02:36,240 They can shrink if we need to shrink them. 36 00:02:36,240 --> 00:02:36,780 Later on. 37 00:02:36,780 --> 00:02:39,150 We realized we didn't have as many values as we thought. 38 00:02:39,930 --> 00:02:46,680 So in this table on the left, this green table, we can see if we were to use standard variable naming 39 00:02:46,680 --> 00:02:49,410 system, we'd have to create five variables. 40 00:02:49,470 --> 00:02:53,490 Student one, student to student three, student four and student five. 41 00:02:53,910 --> 00:02:57,000 We do then have to assign each one of those variables of value. 42 00:02:57,390 --> 00:03:02,580 And later in our code, we'd have to remember which variable meant which name and know which variable 43 00:03:02,580 --> 00:03:05,820 to be referencing at whatever time we need in our code. 44 00:03:06,510 --> 00:03:10,390 If we did this with an array, however, we could create one array variable. 45 00:03:10,560 --> 00:03:15,960 In this case, it would be students and then each of those items in the array could get assigned a value. 46 00:03:16,350 --> 00:03:22,260 So what's one variable declaration compared to the five variable declarations that we would need on 47 00:03:22,260 --> 00:03:22,830 the left? 48 00:03:24,530 --> 00:03:28,530 And this leads into the difference between static and dynamic arrays. 49 00:03:29,490 --> 00:03:32,130 So static arrays have a preset size. 50 00:03:32,150 --> 00:03:35,490 That means they can only take a certain number of items. 51 00:03:35,520 --> 00:03:37,110 And we'll tell the system ahead of time. 52 00:03:37,470 --> 00:03:41,300 Hey, this array that we're about to build for you, it's only going to have four items in it. 53 00:03:41,550 --> 00:03:44,070 All you only ever need to expect four items. 54 00:03:44,430 --> 00:03:49,500 So static arrays are good for when, you know, ahead of time exactly how many items you're going to 55 00:03:49,500 --> 00:03:50,790 have in that array. 56 00:03:51,360 --> 00:03:55,410 But if you had a list and you weren't sure how long your list was and you weren't sure if your list 57 00:03:55,410 --> 00:04:00,150 was actually going to grow or if it might shrink later on, you could use a dynamic array. 58 00:04:00,480 --> 00:04:03,520 And the dynamic Rakan is exactly as it sounds. 59 00:04:03,540 --> 00:04:06,810 Grow and shrink as you add things or take things out. 60 00:04:07,290 --> 00:04:10,740 So in the previous examples, we had exactly five names. 61 00:04:10,740 --> 00:04:12,330 We knew how many names there were going to be. 62 00:04:12,690 --> 00:04:16,440 So we could declare our array as a static array. 63 00:04:16,890 --> 00:04:22,290 Meaning when we declared the array, we tell the system ahead of time recreating an array variable called 64 00:04:22,290 --> 00:04:22,980 students. 65 00:04:23,310 --> 00:04:30,750 And it's going to have zero to four, meaning zero one, two, three and four, all as items within 66 00:04:30,750 --> 00:04:31,380 this array. 67 00:04:31,860 --> 00:04:33,840 And it's going to be a string type variable. 68 00:04:34,200 --> 00:04:35,490 That would be a static array. 69 00:04:35,550 --> 00:04:36,870 This system knows ahead of time. 70 00:04:36,900 --> 00:04:37,230 All right. 71 00:04:37,260 --> 00:04:39,480 I should have index numbers from zero to four. 72 00:04:39,510 --> 00:04:41,850 And that's the only size that this array should ever be. 73 00:04:42,270 --> 00:04:46,440 But if we were worried, for example, that we might have more students in the future or that some of 74 00:04:46,440 --> 00:04:51,570 our existing students, by graduate, we could set up that array to be dynamic, meaning can grow and 75 00:04:51,570 --> 00:04:54,630 shrink depending on however many values we feed that array. 76 00:04:55,020 --> 00:04:58,110 And in this case, when we create the array, we just don't give it a size. 77 00:04:58,140 --> 00:05:00,960 We let the system determine the size as we go. 78 00:05:01,740 --> 00:05:04,170 So in this video, we talked about VBA arrays. 79 00:05:04,500 --> 00:05:06,930 We talked about how they work, why we would use them. 80 00:05:06,990 --> 00:05:09,300 The difference between static and dynamic arrays. 81 00:05:09,630 --> 00:05:14,160 And now we're ready to go ahead and take a look at how to use arrays in visual basic.