{"id":2880,"date":"2017-10-11T00:33:48","date_gmt":"2017-10-11T00:33:48","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=2880"},"modified":"2020-04-23T11:01:24","modified_gmt":"2020-04-23T11:01:24","slug":"excel-vba-lesson-6-mathematical-functions","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-6-mathematical-functions\/","title":{"rendered":"Excel VBA Lesson 6: Mathematical Functions"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-4-operators-in-excel-vba\/\">&lt;&lt;Lesson 5&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-7-trigonometric-functions\/\">&lt;&lt;Lesson 7&gt;&gt;<\/a><\/strong><\/h4>\n<p>In Excel VBA macro programming, we can write codes that can perform arithmetic operations using standard arithmetic operators.<br \/>\nHowever, for more complex mathematical calculations, we need to use the built-in mathematical functions in Excel VBA.<\/p>\n<p>There are numerous built-in mathematical functions in Excel VBA.<br \/>\nAmong them are Abs, Exp, Int, Fix, Rnd, Round, sqr and more. We shall deal with trigonometric functions and Financial Functions in coming lessons.<\/p>\n<h3>6.1 The Abs Function<\/h3>\n<p>In Excel VBA, the Abs function returns the absolute value(positive value) of a given number.<br \/>\nThe syntax is<\/p>\n<p>Abs(Number)<\/p>\n<h4>Example 6.1<\/h4>\n<p>Private Sub CommandButton1_Click()<\/p>\n<p>Cells(1,1)=Abs(-100)<\/p>\n<p>End Sub<\/p>\n<p>Running the program will display 100 in cell A1<\/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><\/p>\n<h3>6.2 The Exp Function<\/h3>\n<p>The Exp of a number x is the value of e<sup>x<\/sup>.The syntax is:<\/p>\n<p>Exp(Number)<\/p>\n<h4>Example 6.2<\/h4>\n<p>Private Sub CommandButton1_Click()<\/p>\n<p>Cells(1,1)=Exp(1)<\/p>\n<p>End Sub<\/p>\n<p>Running the program will displays 2.718282 in cell A1<\/p>\n<h3>6.3 The Int Function<\/h3>\n<p>Int is the function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than the number.<\/p>\n<p>The syntax is<\/p>\n<p>Int(Number)<\/p>\n<h4>Example 6.3<\/h4>\n<p>Private Sub CommandButton1_Click()<\/p>\n<p>Cells(1,1)=Int(2.4)<\/p>\n<p>Cells(2,1)=Int(4.8)<\/p>\n<p>Cells(3,1)=Int(-4.6)<\/p>\n<p>Cells(4,1)=Int(0.32)<\/p>\n<p>End Sub<\/p>\n<p>Running the program will display the results as 2 in cell A1,4 in cell A2,-5 in cell A3 and 0 in cell A4.<\/p>\n<h3>6.4 The Fix Function<\/h3>\n<p>Fix and Int is the same if the number is a positive number as both truncate the decimal part of the number and return an integer.<br \/>\nHowever, when the number is negative, it will return the smallest integer that is larger than the number. The syntax is:<\/p>\n<p>Fix(number)<\/p>\n<h4>Example 6.4<\/h4>\n<p>Private Sub CommandButton1_Click()<\/p>\n<p>Cells(1,1)=Fix(2.4)<\/p>\n<p>Cells(2,1)=Fix(4.8)<\/p>\n<p>Cells(3,1)=Fix(-4.6)<\/p>\n<p>Cells(4,1)=Fix(-6.32)<\/p>\n<p>End Sub<\/p>\n<p>Running the program will displays the results as 2 in cell A1,4 in cell A2,-4 in cell A3 and -6 in cell A4.<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<h3>6.5 The Rnd Function<\/h3>\n<p>The Rnd function returns a random value between 0 and 1.Rnd is very useful when we deal with the concept of chance and probability.<br \/>\nThe syntax is:<\/p>\n<p>Rnd<\/p>\n<h4>Example 6.5<\/h4>\n<p>Private Sub CommandButton1_Click()<\/p>\n<p>Dim x As Integer<\/p>\n<p>For x = 1 To 10<\/p>\n<p>nbsp;Cells(x, 1) = Rnd()<\/p>\n<p>Next x<\/p>\n<p>End Sub<\/p>\n<p>Running the program will display ten random numbers between 0 and 1 from cell A1 to cell A10.<\/p>\n<h3>6.6 The Round Function<\/h3>\n<p>Round is the function that rounds up a number to a certain number of decimal places. The Format is Round (n, m) which means to round a number n to m decimal places. For example, Round (7.2567, 2) =7.26<\/p>\n<h3>6.6 The Sqr Function<\/h3>\n<p>Sqr is the function that computes the square root of a number. For example, Sqr(4)=2, Sqr(9)=2 and etc.<\/p>\n<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-5-sub-procedure-functions\/\">&lt;&lt;Lesson 5&gt;&gt;<\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\"> [Contents] <\/a><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-21\/\">&lt;&lt;Lesson 7&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 5&gt;&gt; [Contents] &lt;&lt;Lesson 7&gt;&gt; In Excel VBA macro programming, we can write codes that can perform arithmetic operations using standard arithmetic operators. However, for more complex mathematical calculations, we need to use the built-in mathematical functions in Excel VBA. There are numerous built-in mathematical functions in Excel VBA. Among them are Abs, Exp, Int, &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-6-mathematical-functions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 6: Mathematical Functions&#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":[14],"tags":[],"class_list":["post-2880","page","type-page","status-publish","hentry","category-function"],"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 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"description\" content=\"Learn how to apply various mathematical functions 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_lesson6.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 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"Learn how to apply various mathematical functions in excel vba macro programming\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson6.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:01:24+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-6-mathematical-functions\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson6.htm\",\"name\":\"Excel VBA Lesson 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"datePublished\":\"2017-10-11T00:33:48+00:00\",\"dateModified\":\"2020-04-23T11:01:24+00:00\",\"description\":\"Learn how to apply various mathematical functions in excel vba macro programming\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson6.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson6.htm\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson6.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 6: Mathematical Functions\"}]},{\"@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 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Learn how to apply various mathematical functions 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_lesson6.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"Learn how to apply various mathematical functions in excel vba macro programming","og_url":"https:\/\/excelvbatutor.com\/vba_lesson6.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T11:01:24+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-6-mathematical-functions\/","url":"https:\/\/excelvbatutor.com\/vba_lesson6.htm","name":"Excel VBA Lesson 6: Mathematical Functions - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"datePublished":"2017-10-11T00:33:48+00:00","dateModified":"2020-04-23T11:01:24+00:00","description":"Learn how to apply various mathematical functions in excel vba macro programming","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson6.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson6.htm"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson6.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 6: Mathematical Functions"}]},{"@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\/2880","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=2880"}],"version-history":[{"count":5,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2880\/revisions"}],"predecessor-version":[{"id":3474,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2880\/revisions\/3474"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}