{"id":2873,"date":"2017-10-11T00:26:00","date_gmt":"2017-10-11T00:26:00","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=2873"},"modified":"2020-04-23T11:00:35","modified_gmt":"2020-04-23T11:00:35","slug":"excel-vba-lesson-4-operators-in-excel-vba","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/","title":{"rendered":"Excel VBA Lesson 4: Operators in Excel VBA"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/\">&lt;&lt;Lesson 3&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-5-sub-procedure-functions\/\">&lt;&lt;Lesson 5&gt;&gt;<\/a><\/strong><\/h4>\n<p>The Operators are important in writing Excel VBA program code. They are used to calculate values, perform certain operations, make comparisons and more. The operators can be divided into three main categories:<\/p>\n<ul>\n<li>Arithmetic<\/li>\n<li>Comparison<\/li>\n<li>Logical<\/li>\n<\/ul>\n<h3>4.1 Arithmetic Operators<\/h3>\n<p>Arithmetic operators are used to perform mathematical operations in Excel VBA.<\/p>\n<h3 align=\"center\">Table 4.1 Arithmetic Operators<\/h3>\n<table border=\"1\" width=\"90%\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n<tbody>\n<tr>\n<th><strong>Operator<\/strong><\/th>\n<th><strong>Mathematical function<\/strong><\/th>\n<th><strong>Example<\/strong><\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">^<\/p>\n<\/td>\n<td>Exponential<\/td>\n<td>MsgBox 2^4 gives a value of 16<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">*<\/p>\n<\/td>\n<td>Multiplication<\/td>\n<td>MsgBox 4*3 gives a value of 12,<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">\/<\/p>\n<\/td>\n<td>Division<\/td>\n<td>MsgBox 12\/4 gives a value of 3<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">Mod<\/p>\n<\/td>\n<td>Modulus (returns the remainder from an integer division)<\/td>\n<td>MsgBox 15 Mod 4 gives value of 3<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">\\<\/p>\n<\/td>\n<td>Integer Division(discards the decimal places)<\/td>\n<td>MsgBox 19\\4 gives a value of 4<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">+ or &amp;<\/p>\n<\/td>\n<td>String concatenation<\/td>\n<td>MsgBox &#8220;Excel&#8221;&amp;&#8221;VBA 2010&#8221; or &#8220;Excel&#8221;+&#8221;VBA 2010&#8221; produces a new string &#8220;Excel VBA 2010&#8221;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\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<p>MsgBox is a built-in function of excel VBA that displays a message. We shall learn more about functions in next lesson. Note that MsgBox 1+&#8221;VBA&#8221; will produce a type mismatch error whereas MsgBox 1&amp;&#8221;VBA&#8221; will not result in an error, it gives a concatenated string 1VBA.<\/p>\n<p>We shall engage the usage of arithmetic operators in Excel VBA code writing in future lessons.<\/p>\n<h3>4.2 Comparison Operators<\/h3>\n<p>Comparison operators are often used in writing code that require decisions making. For example,<\/p>\n<p>If mark&gt;50 then<br \/>\nMsgBox &#8220;Pass&#8221;<br \/>\nElse<br \/>\nMsgBox &#8220;Fail&#8221;<br \/>\nEndif<\/p>\n<p>We shall learn more about writing decision-making code in future lessons.<\/p>\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><br \/>\nThe comparison operators are shown in Table 4.2.<\/p>\n<p>&nbsp;<\/p>\n<h3 align=\"center\">Table 4.2 Comparison Operators<\/h3>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n<tbody>\n<tr>\n<th>\n<p align=\"center\"><strong>Operator<\/strong><\/p>\n<\/th>\n<th><strong>Meaning<\/strong><\/th>\n<th><strong>Example<\/strong><\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">&lt;<\/p>\n<\/td>\n<td>Less than<\/td>\n<td>MsgBox 2&lt;3 returns true while MsgBox 4&gt;5 returns false<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">&lt;=<\/p>\n<\/td>\n<td>Less than or equal to<\/td>\n<td>MsgBox 3&lt;=4 returns true<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">&gt;<\/p>\n<\/td>\n<td>Greater than<\/td>\n<td>MsgBox 5&gt;4 returns true<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">&gt;=<\/p>\n<\/td>\n<td>Greater than or equal to<\/td>\n<td>MsgBox 10&gt;=9 returns true<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">=<\/p>\n<\/td>\n<td>Equal to<\/td>\n<td>MsgBox 10=10 returns true<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">&lt;&gt;<\/p>\n<\/td>\n<td>Not Equal to<\/td>\n<td>MsgBox 9&lt;&gt;10 returns true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>* For letters, the hierarchy is A&gt;B&gt;C&gt;&#8230;&#8230;&#8230;.&gt;Z<\/p>\n<p>Therefore MsgBox A&gt;B returns true<\/p>\n<h3>4.3 Logical Operators<\/h3>\n<p>Logical operators are also used in writing decision-making codes by comparing values or expressions.<\/p>\n<p>&nbsp;<\/p>\n<h3 align=\"center\">Table 4.3 Logical Operators<\/h3>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n<tbody>\n<tr>\n<th>\n<p align=\"center\"><strong>Operator<\/strong><\/p>\n<\/th>\n<th><strong>Meaning<\/strong><\/th>\n<th><strong>Example<\/strong><\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">And<\/p>\n<\/td>\n<td>Logical Conjunction<\/td>\n<td>If A&gt;=80 And B&lt;101 thenGrade=&#8221;A&#8221;<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">Or<\/p>\n<\/td>\n<td>Logical Disjunction<\/td>\n<td>If income&gt;5000 or car&gt;2 thenStatus=&#8221;Rich&#8221;<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">Not<\/p>\n<\/td>\n<td>Logical negation<\/td>\n<td>MsgBox Not (3 &gt; 4)returns true<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"center\">Xor<\/p>\n<\/td>\n<td>Similar to Or, except that it returns False if both camparison values are true<\/td>\n<td>MsgBox 4 &gt; 3 Xor 5 &gt;2 returns false<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-3-array\/\">&lt;&lt;Lesson 3&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-5-sub-procedure-functions\/\">&lt;&lt;Lesson 5&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 3&gt;&gt; [Contents] &lt;&lt;Lesson 5&gt;&gt; The Operators are important in writing Excel VBA program code. They are used to calculate values, perform certain operations, make comparisons and more. The operators can be divided into three main categories: Arithmetic Comparison Logical 4.1 Arithmetic Operators Arithmetic operators are used to perform mathematical operations in Excel VBA. Table &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 4: Operators 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":[18],"tags":[],"class_list":["post-2873","page","type-page","status-publish","hentry","category-operators"],"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 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"Learn how to use various operators 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_lesson4.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 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"Learn how to use various operators in Excel VBA macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson4.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:35+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 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-4-operators-in-excel-vba\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson4.htm\",\"name\":\"Excel VBA Lesson 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"datePublished\":\"2017-10-11T00:26:00+00:00\",\"dateModified\":\"2020-04-23T11:00:35+00:00\",\"description\":\"Learn how to use various operators in Excel VBA macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson4.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson4.htm\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson4.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 4: Operators 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 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Learn how to use various operators 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_lesson4.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"Learn how to use various operators in Excel VBA macro programming","og_url":"https:\/\/excelvbatutor.com\/vba_lesson4.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T11:00:35+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/","url":"https:\/\/excelvbatutor.com\/vba_lesson4.htm","name":"Excel VBA Lesson 4: Operators in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"datePublished":"2017-10-11T00:26:00+00:00","dateModified":"2020-04-23T11:00:35+00:00","description":"Learn how to use various operators in Excel VBA macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson4.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson4.htm"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson4.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 4: Operators 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\/2873","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=2873"}],"version-history":[{"count":7,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2873\/revisions"}],"predecessor-version":[{"id":3472,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2873\/revisions\/3472"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}