{"id":251,"date":"2019-01-09T16:50:24","date_gmt":"2019-01-09T16:50:24","guid":{"rendered":"http:\/\/www.vbastring.com\/blog\/?p=251"},"modified":"2019-01-10T18:15:38","modified_gmt":"2019-01-10T18:15:38","slug":"how-do-i-create-a-user-defined-function-in-excel-vba","status":"publish","type":"post","link":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/","title":{"rendered":"How do I create a user defined function in Excel VBA?"},"content":{"rendered":"<p>There are some things you cannot do easily in Excel without being able to create a user defined function.<\/p>\n<p>User defined functions allow you to enhance the capabilities of Excel.  <\/p>\n<p>But <em>&#8220;How do I create a user defined function in Excel VBA?&#8221;<\/em><\/p>\n<p>Consider this example:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png?resize=489%2C119\" alt=\"custom_function_vba_1\" width=\"489\" height=\"119\" class=\"alignleft size-full wp-image-252\" \/><\/a><\/p>\n<p>You could probably work out a way to get this result, but why not just create a udf, (user define function)? <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"lang:vb decode:true \" >Const dblTax As Double = 0.0825\r\n\r\n\r\nFunction GetGTotal(Price) As Double\r\n    Dim dblGtotal As Double\r\n    \r\n    \r\n    dblGtotal = (Price * dblTax) + Price\r\n    \r\n    GetGTotal = dblGtotal\r\n    \r\nEnd Function<\/pre>\n<p><strong>In the destination cell (B2), you&#8217;ll just type in <em>&#8220;=GetGTotal(B1)&#8221;<\/em><\/strong><\/p>\n<p>Also:<\/p>\n<p>In this example, we are getting a total of the sales values and then calculating the commission for the total sales amount:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_2-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_2-1.png?resize=398%2C283\" alt=\"\" width=\"398\" height=\"283\" class=\"alignleft size-full wp-image-261\" \/><\/a><\/p>\n<pre class=\"lang:vb decode:true \" >Function GetCommission(total) As Double\r\n    Dim dblCommission As Double\r\n    \r\n    dblCommission = total * 0.1\r\n    \r\n    GetCommission = dblCommission\r\n    \r\n    \r\nEnd Function\r\n\r\n<\/pre>\n<p><strong>In the destination cell (A10), you&#8217;ll just type in <em>&#8220;=getcommission(A9)&#8221;<\/em><\/strong><\/p>\n<p><a href=\"http:\/\/www.vbastring.com\/blog\/contact-me\">Click here and let me know if you have any questions.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are some things you cannot do easily in Excel without being able to create a user defined function. User defined functions allow you to enhance the capabilities of Excel. But &#8220;How do I create a user defined function in Excel VBA?&#8221; Consider this example: You could probably work out a way to get this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-251","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do I create a user defined function in Excel VBA? - My Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I create a user defined function in Excel VBA? - My Blog\" \/>\n<meta property=\"og:description\" content=\"There are some things you cannot do easily in Excel without being able to create a user defined function. User defined functions allow you to enhance the capabilities of Excel. But &#8220;How do I create a user defined function in Excel VBA?&#8221; Consider this example: You could probably work out a way to get this [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"My Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-09T16:50:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-10T18:15:38+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"headline\":\"How do I create a user defined function in Excel VBA?\",\"datePublished\":\"2019-01-09T16:50:24+00:00\",\"dateModified\":\"2019-01-10T18:15:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/\"},\"wordCount\":139,\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/custom_function_vba_1.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/\",\"name\":\"How do I create a user defined function in Excel VBA? - My Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/custom_function_vba_1.png\",\"datePublished\":\"2019-01-09T16:50:24+00:00\",\"dateModified\":\"2019-01-10T18:15:38+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/custom_function_vba_1.png\",\"contentUrl\":\"http:\\\/\\\/www.vbastring.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/custom_function_vba_1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/2019\\\/01\\\/09\\\/how-do-i-create-a-user-defined-function-in-excel-vba\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I create a user defined function in Excel VBA?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/\",\"name\":\"My Blog\",\"description\":\"My WordPress Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/#\\\/schema\\\/person\\\/e1de0b30e98940381697872449c341f5\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"https:\\\/\\\/vbastring.com\\\/blog\"],\"url\":\"https:\\\/\\\/vbastring.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do I create a user defined function in Excel VBA? - My Blog","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:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"How do I create a user defined function in Excel VBA? - My Blog","og_description":"There are some things you cannot do easily in Excel without being able to create a user defined function. User defined functions allow you to enhance the capabilities of Excel. But &#8220;How do I create a user defined function in Excel VBA?&#8221; Consider this example: You could probably work out a way to get this [&hellip;]","og_url":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/","og_site_name":"My Blog","article_published_time":"2019-01-09T16:50:24+00:00","article_modified_time":"2019-01-10T18:15:38+00:00","og_image":[{"url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#article","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/"},"author":{"name":"admin","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"headline":"How do I create a user defined function in Excel VBA?","datePublished":"2019-01-09T16:50:24+00:00","dateModified":"2019-01-10T18:15:38+00:00","mainEntityOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/"},"wordCount":139,"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/","url":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/","name":"How do I create a user defined function in Excel VBA? - My Blog","isPartOf":{"@id":"https:\/\/vbastring.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#primaryimage"},"image":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#primaryimage"},"thumbnailUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png","datePublished":"2019-01-09T16:50:24+00:00","dateModified":"2019-01-10T18:15:38+00:00","author":{"@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5"},"breadcrumb":{"@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#primaryimage","url":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png","contentUrl":"http:\/\/www.vbastring.com\/blog\/wp-content\/uploads\/2019\/01\/custom_function_vba_1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vbastring.com\/blog\/2019\/01\/09\/how-do-i-create-a-user-defined-function-in-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vbastring.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How do I create a user defined function in Excel VBA?"}]},{"@type":"WebSite","@id":"https:\/\/vbastring.com\/blog\/#website","url":"https:\/\/vbastring.com\/blog\/","name":"My Blog","description":"My WordPress Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vbastring.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/vbastring.com\/blog\/#\/schema\/person\/e1de0b30e98940381697872449c341f5","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b2feaa698a4bd91b409df1beb5ff6acc2d7842d4f78543d413ebd468bc0171af?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/vbastring.com\/blog"],"url":"https:\/\/vbastring.com\/blog\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":5,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":267,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/posts\/251\/revisions\/267"}],"wp:attachment":[{"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vbastring.com\/blog\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}