<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>spreadsheet-automation on S Anand</title>
    <link>https://www.s-anand.net/blog/tag/spreadsheet-automation/</link>
    <description>Recent content in spreadsheet-automation on S Anand</description>
    <generator>Hugo -- 0.164.0</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 23 Mar 2009 15:39:30 +0000</lastBuildDate>
    <atom:link href="https://www.s-anand.net/blog/tag/spreadsheet-automation/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>User-defined functions in Excel</title>
      <link>https://www.s-anand.net/blog/user-defined-functions-in-excel/</link>
      <pubDate>Fri, 01 Sep 2006 12:00:00 +0000</pubDate>
      <guid>https://www.s-anand.net/blog/user-defined-functions-in-excel/</guid>
      <description>&lt;p&gt;Excel lets you create your own functions. If you wanted to create a function that returned the distance between two points (x1,y1) and (x2,y2), you can create a function DIST that takes these 4 parameters, and use it as shown below.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-example-of-a-user-defined-function-in-excel_231273003_o-gif.webp&#34;&gt;&lt;img alt=&#34;Example of a user-defined function in Excel&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-example-of-a-user-defined-function-in-excel_231273003_o-gif.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To create such a function,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;press Alt-F11 to open the Visual Basic Editor&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;insert a new module (Alt-I-M)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;type the following code:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-visual-basic-code-for-the-dist-user-defined-excel-function_231278952_o-gif.webp&#34;&gt;&lt;img alt=&#34;Visual Basic code for the DIST user-defined Excel function&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-visual-basic-code-for-the-dist-user-defined-excel-function_231278952_o-gif.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Anything you declare as a &amp;ldquo;Function&amp;rdquo; in Excel&amp;rsquo;s Visual Basic automatically becomes visible in the Insert-Function dialog box under the category &amp;ldquo;User Defined&amp;rdquo; (see &lt;a href=&#34;http://www.dailydoseofexcel.com/archives/category/vba/user-defined-functions/&#34;&gt;examples&lt;/a&gt;). The function is normally saved with the file. This is a good idea if you&amp;rsquo;re going to distribute the file. You can also save your functions in your &lt;a href=&#34;http://www.google.com/search?q=personal.xls&amp;amp;btnI=I&#39;m+Feeling+Lucky&#34;&gt;personal.xls&lt;/a&gt; file and load it on startup.&lt;/p&gt;
&lt;p&gt;There are 3 places where I suggest using UDFs.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;You need to repeat a formula or use an additional cell to get the job done (e.g. replace Excel errors with empty strings)&lt;/li&gt;
&lt;li&gt;You &lt;a href=&#34;https://www.s-anand.net/blog/user-defined-functions-to-get-cell-formatting/&#34;&gt;can&amp;rsquo;t get the information&lt;/a&gt; from a formula (e.g. a cell&amp;rsquo;s background colour)&lt;/li&gt;
&lt;li&gt;It&amp;rsquo;s very cumbersome to get the information using formulas (e.g. regular expressions)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;rsquo;s take the first one: &lt;strong&gt;replace Excel errors with empty strings&lt;/strong&gt;. Normally, you&amp;rsquo;d store the results in a cell (say A2), and have another cell with the formula =IF(ISERROR(A2),&amp;quot;&amp;quot;,A2). Instead, create this function NOERROR:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-function-noerror-in-excel-visual-basic_231297813_o-gif.webp&#34;&gt;&lt;img alt=&#34;Function NOERROR in Excel Visual Basic&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-function-noerror-in-excel-visual-basic_231297813_o-gif.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now you can enclose any Excel function inside a NOERROR() and it&amp;rsquo;ll filter out the errors.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-how-the-noerror-function-is-used_231297815_o-gif.webp&#34;&gt;&lt;img alt=&#34;How the NOERROR function is used&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-how-the-noerror-function-is-used_231297815_o-gif.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Notice that cell E2 would&amp;rsquo;ve had a #N/A error if you&amp;rsquo;d just used the VLOOKUP. This function also filters out #REF, #DIV/0!, #NAME? and all other errors.&lt;/p&gt;
&lt;p&gt;BTW, you see column F displaying the formula in column E. I didn&amp;rsquo;t type it out. That&amp;rsquo;s another UDF.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-formulastring-function-returns-the-formula-of-a-cell_231297817_o-gif.webp&#34;&gt;&lt;img alt=&#34;FormulaString function returns the formula of a cell&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-formulastring-function-returns-the-formula-of-a-cell_231297817_o-gif.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I will talk about the other two places where you use UDFs tomorrow.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;comments&#34;&gt;Comments&lt;/h2&gt;
&lt;!-- wp-comments-start --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Greg&lt;/strong&gt; &lt;em&gt;4 Apr 2007 3:15 pm&lt;/em&gt;:
This is very helpful! I am having a problem however; I defined the functions in personal.xls (loaded upon startup) and they always work there, but they aren&amp;rsquo;t available when working with a different file (I get the #NAME error). I also then created variants of them in the specific file, but although they worked, once I made some changes in the file (added a column), they no longer worked even though I re-did the reference properly. Any ideas?&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- wp-comments-end --&gt;
</description>
    </item>
    <item>
      <title>Excel - Avoid manual labour 5</title>
      <link>https://www.s-anand.net/blog/excel-avoid-manual-labour-5/</link>
      <pubDate>Wed, 21 Dec 2005 12:00:00 +0000</pubDate>
      <guid>https://www.s-anand.net/blog/excel-avoid-manual-labour-5/</guid>
      <description>&lt;p&gt;A few tips, without getting into the details. &lt;a href=&#34;http://www.google.com/search?q=excel+array+formulas&#34;&gt;Array formulas&lt;/a&gt; let you perform any calculation on an entire list, and get the result in a single formula. &lt;a href=&#34;http://www.google.com/search?q=excel+database+functions+DSUM&#34;&gt;Database functions&lt;/a&gt; like DSUM can manipulate tables of data very flexibly. Using these and &lt;a href=&#34;http://www.google.com/search?q=excel+pivot+tables&#34;&gt;Pivot tables&lt;/a&gt;, you can do anything with any tabular data.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;comments&#34;&gt;Comments&lt;/h2&gt;
&lt;!-- wp-comments-start --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ananth&lt;/strong&gt; &lt;em&gt;22 Dec 2005 6:32 am&lt;/em&gt;:
I needed your feedback on &lt;a href=&#34;https://www.doondo.org&#34;&gt;www.doondo.org&lt;/a&gt;. Give it a spin let me know. I wanted to avoid positng here but never got any replies for the mails sent at root_node at yahoo.com&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ananth&lt;/strong&gt; &lt;em&gt;22 Dec 2005 6:33 am&lt;/em&gt;:
&lt;a href=&#34;https://www.doondo.org&#34;&gt;www.doondo.org&lt;/a&gt; is a hobby project I&amp;rsquo;ve been working on. The beta is just out.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ananth&lt;/strong&gt; &lt;em&gt;22 Dec 2005 6:35 am&lt;/em&gt;:
Looking forward for your views and feedback&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ritzkini&lt;/strong&gt; &lt;em&gt;22 Dec 2005 10:01 am&lt;/em&gt;:
great concept ananth..shld be a hit considering the great response to a similar funda used in IIM-I&amp;rsquo;s Klueless&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;S Anand&lt;/strong&gt; &lt;em&gt;22 Dec 2005 12:31 pm&lt;/em&gt;:
I liked the concept and interface, Ananth. Will mail you in detail. Quick question&amp;hellip; how do I find out what the right keywords are?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ritzkini&lt;/strong&gt; &lt;em&gt;23 Dec 2005 2:19 am&lt;/em&gt;:
hehehe&amp;hellip;hey anand,saw the visitor stats thing for the first time today ! awesomely cool ,kudos! u did it yourself ??&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dhar&lt;/strong&gt; &lt;em&gt;23 Dec 2005 3:26 am&lt;/em&gt;:
Ananth, does your game support advanced Google operators?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ananth&lt;/strong&gt; &lt;em&gt;23 Dec 2005 4:11 am&lt;/em&gt;:
Thanks for feedback folks. doondo needs additional features, like game summary with right keywords. Right now only basic operators are supported. Even such a simple concept requires complex coding skills! I would love if you can post the suggestion at &lt;a href=&#34;mailto:doondo@googlegroups.com&#34;&gt;doondo@googlegroups.com&lt;/a&gt;. Great if you can join the group. Anand wouldn&amp;rsquo;t like spamming his comments section with doondo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;S Anand&lt;/strong&gt; &lt;em&gt;24 Dec 2005 1:42 am&lt;/em&gt;:
Yeah ritzkini, spent a week coding the visitor stats. What else do you think would be useful to see?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ritzkini&lt;/strong&gt; &lt;em&gt;26 Dec 2005 7:22 am&lt;/em&gt;:
:) i dont think it could have been any more comprehensive than it already is ! :)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;harish&lt;/strong&gt; &lt;em&gt;11 Jan 2006 3:50 am&lt;/em&gt;:
great work mate&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- wp-comments-end --&gt;
</description>
    </item>
    <item>
      <title>Excel - Avoid manual labour 3</title>
      <link>https://www.s-anand.net/blog/excel-avoid-manual-labour-3/</link>
      <pubDate>Fri, 16 Dec 2005 12:00:00 +0000</pubDate>
      <guid>https://www.s-anand.net/blog/excel-avoid-manual-labour-3/</guid>
      <description>&lt;p&gt;A &lt;strong&gt;corollary of Rule 3: Never type the same formula twice&lt;/strong&gt;. Design the formula so that if you cut and paste it elsewhere, it works correctly. The $ symbol and the F4 key for cell references help in 90% of the cases. For complex requirements and large data, 5 functions come in handy: INDIRECT, OFFSET, ADDRESS, ROW and COLUMN.&lt;/p&gt;
&lt;p&gt;I once did a survey, and had data spread across 300 sheets (same format on all sheets). I needed cell D3 across all sheets in a column, to summarise the results. The image explains what I did.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181217_o-png.webp&#34;&gt;&lt;img alt=&#34;Excel snapshot&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181217_o-png.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;INDIRECT returns the value of a cell. INDIRECT(&amp;ldquo;Sheet2!D3&amp;rdquo;) is the value of cell D3 in Sheet2. And INDIRECT(CONCATENATE(A2,&amp;quot;!D3&amp;quot;)) will give you the value of cell D3 in whatever sheet A2 specifies! I created a list of sheet names in column A, and column B had &amp;ldquo;D3&amp;rdquo; in each of those sheets. In effect, &lt;strong&gt;INDIRECT can transpose sheets into columns&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Getting a list of sheet names on to a column is tough, however. If your sheets are sequentially numbered, this image shows a trick that may help.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181204_o-png.webp&#34;&gt;&lt;img alt=&#34;Excel snapshot&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181204_o-png.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you need multiple cells from a sheet, say D3:Z3, use the ADDRESS, ROW and COLUMN functions. ROW(D3) returns 3, and COLUMN(D3) returns 4 &amp;ndash; the respective row and column. If you copy ROW(D3) to multiple rows, you will see ROW(D3), ROW(D4), ROW(D5), &amp;hellip; which are 3, 4, 5 respectively. Similarly for COLUMN. It&amp;rsquo;s a useful way of linking values to position.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181223_o-png.webp&#34;&gt;&lt;img alt=&#34;Excel snapshot&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181223_o-png.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;ADDRESS does the opposite. ADDRESS(ROW(D3),COLUMN(D3)) = ADDRESS(3,4) = &amp;ldquo;$D$3&amp;rdquo;. ADDRESS(3,4,1,1,&amp;ldquo;Sheet2&amp;rdquo;) returns &amp;ldquo;Sheet2!$D$3&amp;rdquo;. (See help for the ,1,1 in the middle, and just put it in always.) To cells D3:Z3 from all the sheets, copy the formula INDIRECT(ADDRESS(3,COLUMN(D3),1,1,$A2)) to the entire range. &lt;strong&gt;The INDIRECT, ADDRESS, ROW/COLUMN combination can slice contiguous data across sheets in any way you want.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Another useful function is OFFSET. OFFSET(D3,2,1) returns the value in cell E5. It shifts the reference D3 down by 2 rows and right by 1 column. OFFSET can be used instead of the INDIRECT and ADDRESS when multiple sheets are not involved. OFFSET can also return a range. OFFSET(D3,0,0,2,2) returns the &lt;strong&gt;range&lt;/strong&gt; D3:E4, which is the 2x2 range starting from 0,0. So SUM(OFFSET(D3,0,0,2,2)) is the same as SUM(D3:E4). With OFFSET, you can specify a range with variable position &lt;strong&gt;and&lt;/strong&gt; variable size (which you can&amp;rsquo;t with $ references).&lt;/p&gt;
&lt;p&gt;Once, we were modelling a leasing company&amp;rsquo;s accounts. (&lt;strong&gt;Warning: this is a complex example.&lt;/strong&gt;) We knew the volume of loans they would disburse over the next 3 years. The monthly interest rate is, say, 1%. What would be their interest income every month? Well, it&amp;rsquo;s not just 1% of what they&amp;rsquo;ve lent out. Customers pay back in equal monthly installments (EMIs). The EMI includes the principal and the interest. Initially, the EMI has a large interest component and very little principal, because there&amp;rsquo;s a lot left to repay. Towards the end, the balance dies down and so does the interest; it&amp;rsquo;s mainly the balance principal that&amp;rsquo;s being repaid. The interest income is not the same every month even for a single lease.&lt;/p&gt;
&lt;p&gt;The calculation is conceptually simple. The IPMT function tells you the monthly interest each month. Let&amp;rsquo;s say all leases are for 36 months. So, to calculate the March interest income, take the January disbursals and multiply it by the third month interest component: IPMT(1%,&lt;strong&gt;3&lt;/strong&gt;,36,-1). Take the Feb disbursals and multiply it by IPMT(1%,&lt;strong&gt;2&lt;/strong&gt;,36,-1). Take the March disbursals and multiply it by IPMT(1%,&lt;strong&gt;1&lt;/strong&gt;,36,-1). And add them up. For April, you&amp;rsquo;d add 4 terms. And so on. Mathematicians call this a convolution. It&amp;rsquo;s like a SUMPRODUCT of a series with another series in reverse.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181202_o-png.webp&#34;&gt;&lt;img alt=&#34;Excel snapshot&#34; loading=&#34;lazy&#34; src=&#34;https://www.s-anand.net/blog/assets/flickr-excel-snapshot_74181202_o-png.webp&#34;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cell E4 on the image alongside does exactly that for month 3 (March). There are 5 columns:&lt;br&gt;
A: Month&lt;br&gt;
B: Amount disbursed that month&lt;br&gt;
C: Months in reverse&lt;br&gt;
D: Interest component for month in reverse&lt;br&gt;
E: Interest income for month&lt;br&gt;
E4 is the sumproduct of B2 to B4 (the Jan, Feb and March disbursals) with something else &amp;ndash; an OFFSET. The offset says, from D1, move down C4 (34) rows and select A4 (3) cells further down. This has the interest components for the first, second and third months in reverse. So, the disbursal for Jan is multiplied with the 3rd month&amp;rsquo;s interest, Feb with the 2nd month&amp;rsquo;s interest, and Mar with the 1st month&amp;rsquo;s interest. That&amp;rsquo;s exactly what we wanted.&lt;/p&gt;
&lt;p&gt;It may look complex. But remember: you have to type this complex formula only once, not 36 times. (And in my case, I had 18 product types.) Also, you&amp;rsquo;re less likely to make typing errors when cutting and pasting. So this saves you debugging time as well.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;comments&#34;&gt;Comments&lt;/h2&gt;
&lt;!-- wp-comments-start --&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ashwin&lt;/strong&gt; &lt;em&gt;18 Dec 2005 6:20 am&lt;/em&gt;:
Anand, even i would like to know about Infosys consulting. Can you mail to me regarding that ?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ashwin&lt;/strong&gt; &lt;em&gt;18 Dec 2005 6:26 am&lt;/em&gt;:
my mail Id is &lt;a href=&#34;mailto:ashwin.cfa@gmail.com&#34;&gt;ashwin.cfa@gmail.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&#34;http://www.alhamour.com&#34;&gt;Al-Hamour&lt;/a&gt;&lt;/strong&gt; &lt;em&gt;23 Mar 2009 8:44 am&lt;/em&gt;:
Hi, I am using the offset function&amp;rsquo;s variable range and wanted to use the ADDRESS and MATCH functions to return the cell reference. However, it is not working when i insert the ADDRESS into the offset. Do you know how to fix this?
=ADDRESS(ROW(),MATCH(BV10,$A$10:$Y$10,0),3)&lt;/li&gt;
&lt;/ul&gt;
&lt;!-- wp-comments-end --&gt;
</description>
    </item>
  </channel>
</rss>
