{"id":64,"date":"2007-01-28T14:02:55","date_gmt":"2007-01-28T13:02:55","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/?p=64"},"modified":"2007-01-28T20:10:06","modified_gmt":"2007-01-28T19:10:06","slug":"adding-custom-functions-to-excel","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=64","title":{"rendered":"Adding custom functions to Excel"},"content":{"rendered":"<p align=\"left\">If you are, like me, programming in different languages, maybe you have also noticed that you miss certain functions in a language, which are available in a different language.<\/p>\n<p align=\"left\">What I am used to do, is either find a function or functions that perform the same task:<\/p>\n<blockquote>\n<p align=\"left\">Delphi: Dec(i);<br \/>PL\/SQL: i := i &#8211; 1;<\/p>\n<\/blockquote>\n<p align=\"left\">or create a function in the language that &#8216;misses&#8217; functionality to help me (or this language ):<\/p>\n<blockquote>\n<p align=\"left\">Delphi: inc(i);<br \/>PL\/SQL:<br \/>create or replace procedure inc(value_in_out in out pls_integer )<br \/>is<br \/>begin<br \/>&nbsp; value_in_out := value_in_out + 1;<br \/>end inc;<\/p>\n<\/blockquote>\n<p align=\"left\">Now I happened to need a function like LPAD in Excel. I couldn&#8217;t find it, so I decided to build it myself. But how can I make sure that my function can be called from a function in a&nbsp;cell?<\/p>\n<p align=\"left\">First of all, I have to create the function. Start the Visual Basic Editor. Select [Insert &#8211; Module] to add a module component to your workbook. In this module add a function that implements the required functionality. Be sure to make this function PUBLIC, so it can be called from a function in a cell.<\/p>\n<blockquote>\n<p align=\"left\">Public Function LPad(AText As String, ALength As Integer, APadchar As String) As String<br \/>&nbsp; LText = AText<br \/>&nbsp; While Len(LText) &lt; ALength<br \/>&nbsp;&nbsp;&nbsp; LText = APadchar + LText<br \/>&nbsp; Wend<br \/>&nbsp; LPad = LText<br \/>&nbsp; Exit Function<br \/>End Function<\/p>\n<\/blockquote>\n<p align=\"left\">In a cell I can now write a function like this:<\/p>\n<blockquote>\n<p align=\"left\">=LPad(&#8220;1&#8243;;10;&#8221;x&#8221;)<\/p>\n<\/blockquote>\n<p align=\"left\">which will result in <\/p>\n<blockquote>\n<p align=\"left\">xxxxxxxxx1<\/p>\n<\/blockquote>\n<p align=\"left\">But you can of course use this function for more complex calculations. I used it to create an import file from an excel document. The import needs a fixed length string, so I needed to LPad cells to their desired length.<\/p>\n<p align=\"left\">Now, what if you want to make this function available to your Excel documents at all times. Follow these steps to Excel happiness \ud83d\ude42<\/p>\n<ol>\n<li>\n<div align=\"left\">start a new document<\/div>\n<\/li>\n<li>\n<div align=\"left\">start the Visual Basic Editor<\/div>\n<\/li>\n<li>\n<div align=\"left\">add a module<\/div>\n<\/li>\n<li>\n<div align=\"left\">add the desired function(s) to the module<\/div>\n<\/li>\n<li>\n<div align=\"left\">save the document as a &#8216;Microsoft Office Excel Add-In (*.xla)&#8217;<\/div>\n<\/li>\n<li>\n<div align=\"left\">be sure to make the location:<br \/><em>C:\\Documents and Settings\\&lt;username&gt;\\Application Data\\Microsoft\\AddIns<\/em><\/div>\n<\/li>\n<\/ol>\n<p align=\"left\">To automagically load the add-in, check your add-in in the Tools-Add-Inns dialog.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are, like me, programming in different languages, maybe you have also noticed that you miss certain functions in a language, which are available in a different language. What I am used to do, is either find a function or functions that perform the same task: Delphi: Dec(i);PL\/SQL: i := i &#8211; 1; or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5,4],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-common","category-plsql","category-tools"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}