Your SlideShare is downloading. ×
0
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Vba ( Visual Basic for Application ) Arrays
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Vba ( Visual Basic for Application ) Arrays

1,215

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,215
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
240
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Developers and Designations Macariao C. Lequin Programmer Resident Chuck Loven Ballacer Designer Amanda Arevalo Documenter Bernadette Francisco & Gregory Dula Presenters John Keno Systems Analyst
  • 2. Introduction Variables refer to storage locations and associated symbolic names that contain some known or unknown quantities, information, or values. A variable's name represents information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same. Variables are like boxes where information can be maintained and referenced. On the outside of the box is a name. When referring to the box, we use the name of the box, not the data stored in the box.
  • 3. Introduction (con’ t) When we work with a single item, we only need to use one variable. However, if we are to work with a number of items, it would be too tasking to use many variables. For example, what if you need to enter 5,000 names. Will you use 5,000 different variables . . . ? You might run out of variables !!!
  • 4. Introduction (con’ t) If we have a list of items which are of similar type to deal with, we use an array of variables instead of using a variable for each item. Arrays make it possible for you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and more simple code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number.
  • 5. Introduction (con’ t) Arrays are useful when you must store a number of values of the same type, but you do not know how many, or you do not want to create individual variables to store them all. For example, suppose you must store a numeric value for every day of the year. You could declare 365 separate numeric variables, but that would be a lot of work. Instead, you can create an array to store all the data in one variable. The array itself is a single variable with multiple elements and each element can contain one piece of data.
  • 6. Current State of Technology Arrays are useful in any programming language when you need to store a large number of values. A student record for teachers is what we intend to create. Some teachers still record their students’ data manually. With our program, we will be able to ease the burden of teachers who record their students’ data manually . Modern, computerized recording of students’ data is faster, more convenient and would minimize errors, if not totally eradicate them.
  • 7. Our Objectives To know the importance of arrays in program development To be able to construct a program using arrays To understand the nature of arrays To appreciate the use of arrays in programming To learn how to manipulate the elements of arrays To be able to create a student record program using arrays arrays in Visual Basic
  • 8. What are Arrays ? An array is a group of variables with the same data type and name. Arrays allow you to gather and manipulate different pieces of information of the same type. We differentiate each item in the array by using subscripts, the index value of each item, for example name (1), name (2), name (3) ... etc.   Arrays can be of any data type. The data type for an array specifies the data type for each element of the array.
  • 9. What is Arrays ? (con’ t) Data Types Every array has a data type, but it is not the same as the data type of its elements. For example, the an array can be of type Integer(), while each of its elements is of type Integer. The notation Integer() means an array of Integer elements.
  • 10. What are Arrays ? (con’ t) Array Dimensions An aray that uses one index and is said to be one-dimensional. An array that uses more than one index or subscript is calledmultidimensional. Another kind of array is one which holds other arrays as elements. This is known as an array of arrays or a jagged array. A jagged array can be either one-dimensional or multidimensional, and so can its elements.
  • 11. Importance of Arrays Arrays play a very important role in almost all programming languages. It provides a very powerful feature which can be used to build a complex type of data structure like the implementation of stacks and queues. Arrays allow us to simplify our processing. We can use arrays to help read and analyze repetitive data with a minimum of coding. An array and a loop can make the program smaller. Arrays enbale the implementation of mathematical vectors, matrices, and other rectangilar tables. Many databases, small and large, consist of or include one-dimensional arrays whose elements are records.  
  • 12. TRIVIA The first digital computers used machine-language programming to set up and access array structures for data tables, vector and matrix computations, and for many other purposes. Atanasoff-Berry computer (ABC) was the first electronic digital computer.I t was built by Vincent Atanasoff and Clifford Berry from Iowa State University between 1937-1942.
  • 13. TRIVIA (con’ t) Merge sort was the first array-sorting program written by John Von Neumann in 1945 during the building of the first stored program computer. Small-Scale Experimental Machine (SSEM), popularly called “Baby”, was the world's first stored-program electronic digital computer that ran its first program on June 21 1948.
  • 14. TRIVIA (con’ t) Array indexing was originally done by self-modifying code, and later using index registers and direct addressing. Some mainframes designed in the 1960s, such as the Burroughs B5000 and its successors, had special instructions for array indexing that included index-bounds checking.
  • 15. TRIVIA (con’ t) Assembly languages generally have no special support for arrays, other than what the machine itself provides. Because of the importance of array structures for efficient computation, earliest high-level programming languages, including FORTRAN (1957), COBOL (1960), ALGOL60 (1960), and C (1972), provided support for multi-dimensional arrays. In C++  (1983), class templates exist for multi-dimensional arrays whose dimension is fixed at runtime as well as for runtime-flexible arrays.
  • 16. Important Points First element of an array has zero index. All the data items in array are always stored in consecutive memory locations. Arrays are always store under a common heading or a variable name An array either be an integer, character, or floating point data item but initialized only on declaration time not afterwards. An array can always be read or write through loops
  • 17. Types of Arrays There are many different types of arrays in Visual Basic for Application. Each type of array has three fundamental attributes, determined at compile time by how the array is declared. Fundamental Attributes Element Data Type Numer of Dimensions Fixed or Dynamic Length
  • 18. Type of Arrays - Element Data Type Arrays have a data type that restricts what can go in the elements of the array. The declared data type of an array is more restrictive that for a variable Only arrays declared with exactly the same data type can be assigned to each other.
  • 19. Type of Arrays – Number of Dimensions Most arrays have only one dimension, but in VBA arrays can have up to 60 dimensions. Additional dimensions are added to an array by declaring multiple sets of bounds separated by commas
  • 20. Type of Arrays – Fixed or Dynamic A fixed-size array is an array whose number of elements is fixed. It is like a CD rack that has a fixed number of slots where you can slide CDs into. You can only place as many CDs as there are slots that will hold it. Use this if you know the exact number of elements that you have.
  • 21. Type of Arrays – Fixed or Dynamic (con’ t) A dynamic array is an array whose number of elements varies. It allows elements to be added or removed from the array. Use this if you do not know the number of elements the array will have. Dynamic arrays can have more than one dimension. Multidimensional dynamic arrays are created by specifying multiple dimension bounds.
  • 22. Types of Arrays (con’ t)
  • 23. Creating Arrays We use the  Dim statement to declare an array just as the way we declare a single variable. Dim arrayName(index) as dataType Name of the array Number of elements in the array Identifies the element to process Type of data each element of the array contains
  • 24. Creating Arrays (con’ t) Dim Student(5) as String Dim Student(5) as String This example creates an array named “Student” with five (5) elements. Each element is of type String. arrayName dataType index Student(1) Student(2) Student(3) Student(4) Student(5)
  • 25. Creating Arrays (con’ t) Another way of declaring an array is to specify the first index and the last index of the array Dim arrayName(first index to last index) as dataType Name of the array Number of elements in the array as specified by the first index up to the last index Type of data each element of the array contains
  • 26. Creating Arrays (con’ t) Dim Student( 1 to 5) as String Dim Student( 1 to 5) as String The previous example was changed to indicate the first index and the last index. This also creates an array named “Student” with five (5) elements, each is of type String. Student(1) Student(2) arrayName Student(3) dataType First index Last index Student(4) Student(5)
  • 27. Creating Arrays (con’ t) You can also declare more than one array in a single line by separating each array with a comma ( , ). Below is an example : Dim Student(3) As String, StudentID(3) As String, StudentGrade(3) As Single Dim Student(3) As String, StudentID(3) As String, StudentGrade(3) As Single First array named “Student” with 3 elements, each of type string Second array named “StudentID” with 3 elements, each of type string Third array named “StudentGrade” with 3 elements, each of type Single (single precision floating point)
  • 28. Creating Arrays (con’ t) Multidimensional arrays can be thought of as arrays-of-arrays. They are often needed when we are dealing with more complex database, especially those that handle large amount of data. Data are usually organized and arranged in table form, this is where multidimensional arrays come into play.
  • 29. Creating Arrays (con’ t)  Two dimensional array can be represented by a table that contains rows and columns, where one index represents the rows and the other index represent the columns. 1st dimension 2nd dimension
  • 30. Creating Arrays (con’ t) The format to declare a 2-dimensional array is shown below :  Dim arrayName ( num1 , num2 ) as dataType Name of the array the suffix of the first dimension of the last element  the suffix of the second dimension of the last element  Type of data each element of the array contains
  • 31. Creating Arrays (con’ t) The suffixes of the element in the array will start with (0, 0) unless you set the Option Base to 1. In the case when the Option Base is set to 1, then the suffixes of the element in the array will start with (1, 1). Dim Score (3,3) as Integer Dim Score (3,3) as Integer Name of the array the suffix of the first dimension of the last element  the suffix of the second dimension of the last element  Type of data each element of the array contains
  • 32. Creating Arrays (con’ t) This example will create a two dimension array consisting of 16 elements. These elements can be organized in a table form as shown in the table below:
  • 33. Creating Arrays (con’ t) With reference to the previous example, if you set the option base to 1, then there will be only 9 elements, that is from Score(1,1) to Score(3,3). If you want the first element to start with suffixes (1,1) you can also use the following format of declaration: Dim Score( 1 to 3, 1 to 3) as Integer Dim Score( 1 to 3, 1 to 3) as Integer Name of the array the suffix of the first dimension of the last element  the suffix of the second dimension of the last element  Type of data each element of the array contains
  • 34. Dynamic Arrays The length of an array can be fixed at declaration by explicitly setting bounds, or can be dynamic if no bounds are specified. If an array has explicit (fixed) bounds it can be used right away, but dynamic arrays must be resized at least once before they can be used. Dynamic arrays are resized using the Redim statement. If the Preserve keyword is supplied, any existing elements already in the array being resized will remain intact. If it is omitted all elements will be erased.
  • 35. Dynamic Arrays (con’ t) To resize the array, you provide a value for the upper bound, and optionally, for the lower bound. The upper and lower bound of an array refer to the beginning and ending indexes for the array. You must specify the upper bound for the array when you resize it. The lower bound is optional, but it is a good idea to include it, so that you know what the lower bound of the array is.
  • 36. Dynamic Arrays (con’ t) The Redim keyword syntax array is shown below :  ReDim [Preserve] ArrayName(Subscript) As DataType ReDim - keyword that denotes we are redimensioning an array. Preserve - optional keyword that is used to retain all existing elements' values. Without it all elements will return to their default values. ArrayName - the name of the array. Subscript  - the dimensions of the array. DataType - any valid data type. The data type cannot be changed from its initial declaration when using the ReDim keyword, unless it was initially declared as a Variant
  • 37. Dynamic Arrays (con’ t) Resizing an array with the Preserve keyword can be slow, so you want to do it as infrequently as possible. A good way to minimize use of the Preserve keyword in your code is to estimate the amount of data you require to store and size the array accordingly. If an error occurs because you have not made the array large enough, you can resize it within the error handler as many times as necessary. When you're through working with the array, if it is larger than you require, you can resize it to make it just large enough to contain the data it currently has.
  • 38. Dynamic Arrays (con’ t) Declares the array Grades as Dynamic Dim Grades( ))As Integer Dim Grades( As Integer Resizes the array Grades to contain 100 elements ReDim Grades(0 to 99) ReDim Grades(0 to 99) Resizes the array Grades to contain 600 elements at the same time retaining the original values of the elements Grades(0) to Grades(99). ReDim Preserve Grades(0 to 599) ReDim Preserve Grades(0 to 599)
  • 39. Erasing Arrays You should always erase your array when you are done using it, especially if you are using dynamic arrays. To erase an array, we use the Erase statement as in : Erase ArrayName
  • 40. Erasing Arrays (con’ t) Declares the array strFriends with 3 elements as String type. Dim strFriends(0 to 2) As String Dim strFriends(0 to 2) As String Assigns values to the 3 elements of the array strFriends(0) = "Bianca" strFriends(0) = "Bianca" strFriends(1) = "Jeana" strFriends(1) = "Jeana" strFriends(2) = "Erin" strFriends(2) = "Erin" Erases the array Erase strFriends) Erase strFriends)
  • 41. Retrieving Contents of Arrays After building an array, we can now retrieve its contents. Below is an example of an array of friends’ names that was created : Dim strFriends(0 to 6) As String Dim strFriends(0 to 6) As String strFriends(0) = "Bianca" strFriends(0) = "Bianca" strFriends(1) = "Jeana" strFriends(1) = "Jeana" strFriends(2) = "Sam" strFriends(2) = "Sam" strFriends(3) = "Jenna" strFriends(3) = "Jenna" strFriends(4) = "Erin" strFriends(4) = "Erin" strFriends(5) = "Carolyn" strFriends(5) = "Carolyn" strFriends(6) = "Kate" strFriends(6) = "Kate"
  • 42. Retrieving Contents of Arrays (con’ t) We want to display these names in successive message boxes, so we construct a loop, using the For . . .Next statement Dim strFriends(0 to 6) As String, lngPosition as Long Dim strFriends(0 to 6) As String, lngPosition as Long strFriends(0) = "Bianca" strFriends(0) = "Bianca" strFriends(1) = "Jeana" strFriends(1) = "Jeana" strFriends(2) = "Sam" strFriends(2) = "Sam" strFriends(3) = "Jenna" strFriends(3) = "Jenna" strFriends(4) = "Erin" strFriends(4) = "Erin" strFriends(5) = "Carolyn" strFriends(5) = "Carolyn" strFriends(6) = "Kate" strFriends(6) = "Kate" For lngPosition = LBound(strFriends) To UBound(strFriends) For lngPosition = LBound(strFriends) To UBound(strFriends) MsgBox strFriends(lngPosition) MsgBox strFriends(lngPosition) Next lngPositionlngPositionlngPosition Next lngPositionlngPositionlngPosition
  • 43. Retrieving Contents of Arrays (con’ t) There are two new functions used in yhis example, Lbound and Ubound. These are used to determine the lower and upper bounds of an array. Since strFriends has a lower bound of 0 and an upper bound of 6, these functions allow you to to iterate through an array with a dynamic size, without having to keep track of the array's size yourself. With each iteration of the loop, lngPosition will count up from 0 to 6. By accessing the array as strFriends(lngPosition) we are greatly reducing the amount of code you have to write.
  • 44. Split and Join The Split function allows us to convert a character string into an array. variable = "IF/VLOOKUP/SUM/COUNT/ISNUMBER/MID" variable = "IF/VLOOKUP/SUM/COUNT/ISNUMBER/MID" Use the Split function and define the separator : en = Split(variable, "/") en = Split(variable, "/")
  • 45. Split and Join (con’ t) The array en will return the following values : MsgBox en(0) '=> returns ::IF MsgBox en(0) '=> returns IF MsgBox en(1) '=> returns ::VLOOKUP MsgBox en(1) '=> returns VLOOKUP MsgBox en(2) '=> returns ::SUM MsgBox en(2) '=> returns SUM MsgBox en(3) '=> returns ::COUNT MsgBox en(3) '=> returns COUNT MsgBox en(4) '=> returns ::ISNUMBER MsgBox en(4) '=> returns ISNUMBER MsgBox en(5) '=> returns ::MID MsgBox en(5) '=> returns MID The following 3 arrays will also return the same values : en = Array("IF", "VLOOKUP", "SUM", "COUNT", "ISNUMBER", "MID") en = Array("IF", "VLOOKUP", "SUM", "COUNT", "ISNUMBER", "MID") en = Split("IF,VLOOKUP,SUM,COUNT,ISNUMBER,MID", ",") en = Split("IF,VLOOKUP,SUM,COUNT,ISNUMBER,MID", ",") en = Split("IF VLOOKUP SUM COUNT ISNUMBER MID", " ") en = Split("IF VLOOKUP SUM COUNT ISNUMBER MID", " ")
  • 46. Split and Join (con’ t) The following example returns the 3rd value in the string : MsgBox Split("IF,VLOOKUP,SUM,COUNT,ISNUMBER,MID", ",")(2) '=> returns : :SUM MsgBox Split("IF,VLOOKUP,SUM,COUNT,ISNUMBER,MID", ",")(2) '=> returns SUM
  • 47. Split and Join (con’ t) The opposite of the Split function is Join. This function assembles the values of an array into a string. MsgBox Join(Array(1, 2, 3, 4, 5), "") '=> returns ::12345 MsgBox Join(Array(1, 2, 3, 4, 5), "") '=> returns 12345
  • 48. Sample Arrays in Excel VBA Private Sub CommandButton1_Click( )) Private Sub CommandButton1_Click( Name") Name") Dim StudentName(1 to 5) As String Dim StudentName(1 to 5) As String For i i= 1 To 5 For = 1 To 5 StudentName(i) = InputBox("Enter student StudentName(i) = InputBox("Enter student Cells(i, 1) = StudentName(i) Cells(i, 1) = StudentName(i) Next Next End Sub End Sub
  • 49. Sample Arrays in Excel VBA (con’ t) In this example, we define an array StudentName of five strings using the Dim keyword. An InputBox is included to accept input from the user. A For ...Next loop is placed to accept the input five times and display the five names from cell A1 to cell E1. When we run the program, an input box will appear five times, allowing the user to enter five names. The sample input box is shown on the next slide.
  • 50. Sample Arrays in Excel VBA (con’ t) The five names will be displayed in the spreadsheet as shown on the next slide.         
  • 51. Sample Arrays in Excel VBA (con’ t)
  • 52. Sample Arrays in Excel VBA (con’ t) Private Sub CommandButton1_Click( )) Private Sub CommandButton1_Click( Dim StudentName(3) As String, StudentID(3) As Dim StudentName(3) As String, StudentID(3) As String, StudentMark(3) As Single String, StudentMark(3) As Single For i i= 1 To 3 For = 1 To 3 StudentName(i) = InputBox("Enter student StudentName(i) = InputBox("Enter student Name") Name") StudentID(i) = InputBox("Enter student ID") StudentID(i) = InputBox("Enter student ID") StudentMark(i) = InputBox("Enter student StudentMark(i) = InputBox("Enter student Mark") Mark") Cells(i, 1) = StudentName(i) Cells(i, 1) = StudentName(i) Cells(i, 2) = StudentID(i) Cells(i, 2) = StudentID(i) Cells(i, 3) = StudentMark(i) Cells(i, 3) = StudentMark(i) Next Next
  • 53. Sample Arrays in Excel VBA (con’ t) This example declares three arrays in a single line. An InputBox and a For …Next loop are again included to accept input from the user A For ...Next loop is placed to accept the input three times and display the three names, ID number, and mark from cell A1 to cell C3. When we run the program, three input boxes will appear consecutively to allow the user to enter the student name, student ID and student mark. The process will repeat three times until three student names, student IDs and student marks have been entered.
  • 54. Sample Arrays in Excel VBA (con’ t) The sample input boxes and the output are shown below and on the succeeding slides.
  • 55. Sample Arrays in Excel VBA (con’ t)
  • 56. Sample Arrays in Excel VBA (con’ t)
  • 57. Sample Arrays in Excel VBA (con’ t)
  • 58. The Environment OK Button – used to go to the Username and Password screen EXIT Button – used to exit the program
  • 59. The Environment (con’ t) Text Box 1 – used to enter User Name Text Box 2 – used to enter user’s password OK Button – used to accept user’s name and password and go to the Main Menu EXIT Button – used to exit the program
  • 60. The Environment (con’ t) Label – indicates the type of array Record Button – enter the Student Record screen EXIT Button – used to exit the program
  • 61. The Environment (con’ t) Dialog Box – used to enter the student’s name, id, quiz1, quiz 2, and final grade OK Button – used to display the next dialog box EXIT Button – exit the program ERASE Button – used to delete or erase the record in EXCEL one by one
  • 62. Conclusion We therefore conclude that the Student Record System : Will introduce to the user a more systematic way of handling students’ records Will help teachers in doing their recording of students’ information and ease their burden Will support teachers in their job by paving the way for a more convenient and efficient (less paper work) method of recording all students’ data.

×