{"id":567,"date":"2013-03-03T15:30:36","date_gmt":"2013-03-03T15:30:36","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=567"},"modified":"2020-04-23T11:00:11","modified_gmt":"2020-04-23T11:00:11","slug":"excel-vba-lesson-3-array","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/","title":{"rendered":"Excel VBA Lesson 3: Working with Array"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-2\/\">&lt;&lt;Lesson 2&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/\">&lt;&lt;Lesson 4&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 Array in Excel VBA<\/strong><\/h3>\n\n\n\n<p>When we work with a single item in Excel VBA, we only need to use one variable. However, if we have to deal with a list of items which are of similar type , we need to declare an array of variables instead of using a variable for each item. For example, if we need to enter one hundred names, instead of declaring one hundred different variables, we need to declare only one array. By definition, an array is a group of variables with the same data type and name. We differentiate each item in the array by using subscript, the index value of each item, for example name (1), name (2), name (3) &#8230;&#8230;.etc.<br>\n<script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 Declaring an Array in Excel VBA<\/strong><\/h3>\n\n\n\n<p>We use the Dim statement to declare an array just as like the way we declare a single variable. In Excel VBA, we can declare a one-dimensional array, two-dimensional array or even a multidimensional array (up to 60)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2.1&nbsp;One-Dimensional Array<\/strong><\/h4>\n\n\n\n<p>The syntax to declare a one-dimensional array in Excel VBA is as follows:<\/p>\n\n\n\n<p>Dim arrayName(index) as dataType or<br>\nDim arrayName(first index to last index) as dataType<\/p>\n\n\n\n<p>For example\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim StudentName(10) as String\nDim StudentName(1 to 10) as String\nDim StudentMark(10) as Single\nDim StudentMark( 1 to 10) as Single\n<\/pre>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 3.1<\/strong><\/h4>\n\n\n\n<p>In this example, we define an array StudentName of five strings using the Dim keyword. We include an InputBox to accept input from the user. We also use the For &#8230;Next loop to accept the input five times and display the five names from cell A1 to cell E1. The code is as follows:\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click( )\nDim StudentName(1 to 5) As String\nFor i = 1 To 5\nStudentName(i) = InputBox(\"Enter student Name\")\nCells(i, 1) = StudentName(i)\nNext\nEnd Sub\n<\/pre>\n\n\n\n<p>* You can also declare the array using Dim StudentName(5) As String<\/p>\n\n\n\n<p>When we run the program, an input box will appear, as shown below. This input box will repeat five times and let the user enter five names.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.1<\/strong><\/p>\n\n\n\n<p>The five names will be displayed in the spreadsheet as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.2<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 3.2<\/strong><\/h4>\n\n\n\n<p>You can also declare more than one array on a single line. In this example, we declare three arrays in a single line, separated by commas.\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click( )\nDim StudentName(3) As String, StudentID(3) As String, StudentMark(3) As Single\nFor i = 1 To 3\nStudentName(i) = InputBox(\"Enter student Name\")\nStudentID(i) = InputBox(\"Enter student ID\")\nStudentMark(i) = InputBox(\"Enter student Mark\")\nCells(i, 1) = StudentName(i)\nCells(i, 2) = StudentID(i)\nCells(i, 3) = StudentMark(i)\nNext\nEnd Sub\n<\/pre>\n\n\n\n<p>When we run the program, three input boxes will appear consecutively to let the user enter the student name, the student ID and then the student mark. The process will repeat three times until the particulars of all three students have been entered. The three input boxes and the output images are shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.3<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.4<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.4.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.5<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.5.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.6<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3.2.2 Two-Dimensional Array<\/strong><\/h4>\n\n\n\n<p>Multidimensional arrays are often needed when we are dealing with the more complex database, especially those that handle a large amount of data. Data are usually organized and arranged in table form, this is where the multidimensional arrays come into play. However, in this tutorial, we are dealing only with the two-dimensional array.The 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.<\/p>\n\n\n\n<p>The syntax to declare a two-dimensional array is\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim arrayName (num1, num2) as datatype\n<\/pre>\n\n\n\n<p>Where num1 is the suffix of the first dimension of the last element and num2 is the suffix of the second dimension of the last element in the array. 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). For example,\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim Score (3, 3) as Integer\n<\/pre>\n\n\n\n<p>Creates a two dimension array consists of 16 elements. These elements can be organized in a table form as shown in the table below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_table21.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"589\" height=\"160\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/03\/vba_table21.1.jpg\" alt=\"vba_table21.1\" class=\"wp-image-574\"\/><\/a><\/figure><\/div>\n\n\n\n<p>If you set the option base to 1, then there will be only 9 elements, i.e from Score(1,1) to Score(3,3). However, if you want the first element to start with suffixes (1,1) you can also use the following format of declaration:<br>\nDim Score(1 to 3, 1 to 3) as Integer<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Example 3.3<\/strong><\/h4>\n\n\n\n<p>If a company wants to track the performance of 5 salespersons over a period of 2 days, you can create a 5&#215;2 array in Excel VBA, denoted by a 5X 2 table in a spreadsheet. Next, you can write the following VBA code:\n<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click()\nDim SalesVolume(2to 6, 2 to 3) as Single\nDim SalesPerson as Integer, Day as Integer\nFor SalesPerson=2 to 6\nFor Day=2 to3\nSalesVolume(SalesPerson, Day)=inputbox(\"Enter Sales Volume\")\nCells(SalesPerson, Day)=SalesVolume(SalesPerson,Day)\nNext Day\nNext SalesPerson\nEnd Sub\n<\/pre>\n\n\n\n<p>When the user runs the program, the inputbox that will prompt the user to enter sales volume will appear 10 times, as shown in the Figure below :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.3.1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.7<\/strong><\/p>\n\n\n\n<p>After all the sales Volumes are entered, the values in the spreadsheet are shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.3.2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.8<\/strong><\/p>\n\n\n\n<p>If you need to make sure the user enters the correct sales volume, you can change the line 5 statement to<br>\nSalesVolume(SalesPerson, Day) = InputBox(&#8220;Enter Sales Volume of &#8221; &amp; &#8221; SalesPerson &#8221; &amp; (SalesPerson &#8211; 1) &amp; &#8221; Day &#8221; &amp; (Day &#8211; 1))<br>\nA clearer instruction will be shown as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/example21.3.3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Figure 3.9<\/strong><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-2\/\">&lt;&lt;Lesson 2&gt;&gt;<\/a>&nbsp;<a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]&nbsp;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/\">&lt;&lt;Lesson 4&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 2&gt;&gt; [Contents] &lt;&lt;Lesson 4&gt;&gt; 3.1 Array in Excel VBA When we work with a single item in Excel VBA, we only need to use one variable. However, if we have to deal with a list of items which are of similar type , we need to declare an array of variables instead of using &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 3: Working with Array&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-567","page","type-page","status-publish","hentry","category-array"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA illustrate how to construct an array in Excel VBA macro programming\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson3.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA illustrate how to construct an array in Excel VBA macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson3.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T11:00:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm\",\"name\":\"Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\",\"datePublished\":\"2013-03-03T15:30:36+00:00\",\"dateModified\":\"2020-04-23T11:00:11+00:00\",\"description\":\"This Excel VBA illustrate how to construct an array in Excel VBA macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson3.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson3.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 3: Working with Array\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA illustrate how to construct an array in Excel VBA macro programming","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/vba_lesson3.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA illustrate how to construct an array in Excel VBA macro programming","og_url":"https:\/\/excelvbatutor.com\/vba_lesson3.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T11:00:11+00:00","og_image":[{"url":"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/","url":"https:\/\/excelvbatutor.com\/vba_lesson3.htm","name":"Excel VBA Lesson 3: Working with Array - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg","datePublished":"2013-03-03T15:30:36+00:00","dateModified":"2020-04-23T11:00:11+00:00","description":"This Excel VBA illustrate how to construct an array in Excel VBA macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson3.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson3.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson3.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/vba_img\/example21.1.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson3.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 3: Working with Array"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=567"}],"version-history":[{"count":38,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/567\/revisions"}],"predecessor-version":[{"id":3471,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/567\/revisions\/3471"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}