{"id":256,"date":"2013-02-26T16:00:20","date_gmt":"2013-02-26T16:00:20","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=256"},"modified":"2020-04-23T10:59:45","modified_gmt":"2020-04-23T10:59:45","slug":"excel-vba-lesson-2","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-2\/","title":{"rendered":"Excel VBA Lesson 2: Working with Variables in Excel VBA"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-1\/\">&lt;&lt;Lesson 1&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/\">&lt;&lt;Lesson 3&gt;&gt;<\/a><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 The Concept of Variables in Excel VBA<\/h3>\n\n\n\n<p>A variable is like a&nbsp; mailbox in the post office as its content changes every now and then, just like the mailboxes. In Excel VBA, variables are areas allocated by the computer memory to hold data. Similar to the mailbox, each variable must be given a name. In order to name a variable in Excel VBA, you have to follow a set of rules, as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It must be less than 255 characters<\/li><li>No spacing is allowed<\/li><li>It must not&nbsp;begin with a number<\/li><li>Period is not permitted<\/li><\/ul>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><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><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><br>Examples of valid and invalid variable names are displayed in Table 2.1<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"146\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\" alt=\"Excel vba table2.1\" class=\"wp-image-261\"\/><\/a><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 Declaring Variables<\/h3>\n\n\n\n<p>In Excel VBA, you need to declare the variables before using them by assigning names and data types. We can divide the Excel VBA data types&nbsp; into two types,&nbsp; the numeric data types and the non-numeric data types<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2.1 Numeric&nbsp;Data Types<\/h4>\n\n\n\n<p>Numeric data types are types of data that consist of numbers. We can compute the numeric data mathematically with&nbsp;various standard operators such as plus, subtract, multiply, divide and so on. In Excel VBA,&nbsp; we can divide the numeric data into 7 types,&nbsp; as shown in Table 2.2<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"853\" height=\"285\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.2.jpg\" alt=\"Excel vba table2.2\" class=\"wp-image-262\"\/><\/a><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2.2.2 Non-numeric Data Types<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.3.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"233\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.3.jpg\" alt=\"vba_table2.3\" class=\"wp-image-263\"\/><\/a><\/figure><\/div>\n\n\n\n<p>The non-numeric&nbsp;data types are summarized in Table 2.3 below.<br><\/p>\n\n\n\n<p>We can declare the variables implicitly or explicitly. For example, sum=text1.text means that the variable sum is declared implicitly and ready to receive the input in the Text1 textbox. In addition, other examples of the implicit declaration are volume=8 and label=\u201dWelcome\u201d. On the other hand, for the explicit declaration, we declare the variables in the general section of the code window by using the Dim statement. The syntax&nbsp;is as follows:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Dim variableName as DataType<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Example 2.1<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim password As String\nDim yourName As String\nDim firstnum As Integer\nDim secondnum As Integer\nDim total As Integer\nDim BirthDay As Date\n<\/pre>\n\n\n\n<p>On the other hand, you may&nbsp; combine them in one line, separating each variable with a comma, as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim password As String,&nbsp;yourName As String, firstnum As Integer.<\/pre>\n\n\n\n<p>If the data type is not specified, Excel VBA will automatically declare the variable as a Variant. For string declaration, there are two possible formats, one for the variable-length string&nbsp;and another for the fixed-length string. For the variable-length string, just use the same format as Example 2.1 above.&nbsp;However, for the fixed-length string, you have to use the format as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Dim VariableName as String * n\n<\/pre>\n\n\n\n<p>where n defines the number of characters the string can hold.&nbsp; For example, Dim yourName as String * 10 mean yourName can hold no more than 10 Characters.<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br><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><script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><b>Example 2.2<\/b><\/h4>\n\n\n\n<p>In this example, we declared three types of variables, namely the string, date and currency.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Private Sub CommandButton1_Click()\n\nDim YourName As String\nDim BirthDay As Date\nDim Income As Currency\n YourName = \"Alex\"\nBirthDay = \"1 April 1980\"\nIncome = 1000\nRange(\"A1\") = YourName\nRange(\"A2\") = BirthDay\nRange(\"A3\") = Income \n\nEnd Sub\n<\/pre>\n\n\n\n<p>The output screen of Example 2.2 is as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/vba_img\/vba_Ex2.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-1\/\">&lt;&lt;Lesson 1&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-3-array\/\">&lt;&lt;Lesson 3&gt;&gt;<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 1&gt;&gt; [Contents] &lt;&lt;Lesson 3&gt;&gt; 2.1 The Concept of Variables in Excel VBA A variable is like a&nbsp; mailbox in the post office as its content changes every now and then, just like the mailboxes. In Excel VBA, variables are areas allocated by the computer memory to hold data. Similar to the mailbox, each variable &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 2: Working with Variables in Excel VBA&#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":[9],"tags":[],"class_list":["post-256","page","type-page","status-publish","hentry","category-variables"],"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 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"This Excel VBA lesson explains how to work with variables in Excel VBA\" \/>\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_lesson2.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 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"This Excel VBA lesson explains how to work with variables in Excel VBA\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson2.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-23T10:59:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"837\" \/>\n\t<meta property=\"og:image:height\" content=\"146\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 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-2\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm\",\"name\":\"Excel VBA Lesson 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\",\"datePublished\":\"2013-02-26T16:00:20+00:00\",\"dateModified\":\"2020-04-23T10:59:45+00:00\",\"description\":\"This Excel VBA lesson explains how to work with variables in Excel VBA\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson2.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg\",\"width\":837,\"height\":146},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson2.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 2: Working with Variables in Excel VBA\"}]},{\"@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 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"This Excel VBA lesson explains how to work with variables in Excel VBA","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_lesson2.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"This Excel VBA lesson explains how to work with variables in Excel VBA","og_url":"https:\/\/excelvbatutor.com\/vba_lesson2.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:59:45+00:00","og_image":[{"width":837,"height":146,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-2\/","url":"https:\/\/excelvbatutor.com\/vba_lesson2.htm","name":"Excel VBA Lesson 2: Working with Variables in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg","datePublished":"2013-02-26T16:00:20+00:00","dateModified":"2020-04-23T10:59:45+00:00","description":"This Excel VBA lesson explains how to work with variables in Excel VBA","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson2.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson2.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson2.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/02\/vba_table2.1.jpg","width":837,"height":146},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson2.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 2: Working with Variables in Excel VBA"}]},{"@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\/256","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=256"}],"version-history":[{"count":43,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/256\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/256\/revisions\/3470"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}