{"id":9153,"date":"2024-04-25T23:33:44","date_gmt":"2024-04-25T23:33:44","guid":{"rendered":"https:\/\/www.hexacorn.com\/blog\/?p=9153"},"modified":"2024-04-25T23:33:44","modified_gmt":"2024-04-25T23:33:44","slug":"excelling-at-excel-part-4","status":"publish","type":"post","link":"https:\/\/www.hexacorn.com\/blog\/2024\/04\/25\/excelling-at-excel-part-4\/","title":{"rendered":"Excelling at Excel, Part 4"},"content":{"rendered":"\n<p>Excel is the emperor of automation. Not the SOAR type, but the local one &#8211; yours.<\/p>\n\n\n\n<p>Why?<\/p>\n\n\n\n<p>Its formulas and VBA capabilities can turn many awfully mundane tasks into plenty of automation opportunities&#8230;<\/p>\n\n\n\n<p>For instance&#8230; certain programming tasks.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Switch_statement\">case\/switch<\/a> syntax is a beautiful construct. It allows us to define a large set of complex if\/then statements in a very elegant way. It is very often used to split a data\/value set into conditions that determine the result\/output\/outcome based on the input.<\/p>\n\n\n\n<p>Now, there are some programming languages that do not support case\/switch statements well (not a part of its functional specification, introduced in late versions hence not fully compatible, etc.).<\/p>\n\n\n\n<p>Writing case\/switch statements for these languages is TOUGH. This is because their limitations often force us to rely on a bunch of NESTED IF clauses&#8230; Writing many of these, nested, is not for the faint-hearted. Typos, incorrect number of opening or closing parenthesis, and overall &#8212; getting lost in the complexity of this nested logic is very easy.<\/p>\n\n\n\n<p>And this is where Excel can help a lot.<\/p>\n\n\n\n<p>Imagine a hypothetical scenario where you have to write a code that gets this input data (names that are a loose set of Value1, Value2, Value3) to generate the output (A, B, C, D, E) based on peoples&#8217; names:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-1.png\"><img decoding=\"async\" loading=\"lazy\" width=\"257\" height=\"121\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-1.png\" alt=\"\" class=\"wp-image-9155\"\/><\/a><\/figure>\n\n\n\n<p>It&#8217;s pretty straightforward in case\/switch supporting programming languages to define rules based on this set, but if the only thing available are nested ifs &#8211; it&#8217;s much harder.<\/p>\n\n\n\n<p>Let&#8217;s try though&#8230;<\/p>\n\n\n\n<p>The first idea we can tackle is to convert these 3 values (some are empty) to an array of per-row list of values:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-2.png\"><img decoding=\"async\" loading=\"lazy\" width=\"414\" height=\"121\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-2.png\" alt=\"\" class=\"wp-image-9156\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-2.png 414w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-2-300x88.png 300w\" sizes=\"(max-width: 414px) 100vw, 414px\" \/><\/a><\/figure>\n\n\n\n<p>You will admit that the <em>List <\/em>column looks more &#8216;programmer friendly&#8217; now.<\/p>\n\n\n\n<p>The formula that produces the values in the <em>List<\/em> column is obscenely simple:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3.png\"><img decoding=\"async\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3-1024x121.png\" alt=\"\" class=\"wp-image-9157\" width=\"518\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3-1024x121.png 1024w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3-300x35.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3-768x91.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3-500x59.png 500w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-3.png 1057w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>We simply build a parenthesis-embraced list of values where the first one is always present, and the two others are added only if the respective cells are not empty.<\/p>\n\n\n\n<p>Trivial!<\/p>\n\n\n\n<p>But how do we convert this list of values into a programmatic construct that gives us A, B, C, D or E depending on the name (one of these three values\/per row)?<\/p>\n\n\n\n<p>This is one way to do it (using ternary operator):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(if (name in ('John', 'Jack', 'Anne') ? 'A' : (if (name in ('Peter') ? 'B' : (if (name in ('Kate', 'Leo') ? 'C' : (if (name in ('Paul', 'Ariel', 'Fyodor') ? 'D' : (if (name in ('Amy', 'Maria') ? 'E' : \"N\/A\"))))))))))\n<\/pre>\n\n\n\n<p>If you are curious where the formula comes from &#8212; it is from the very same spreadsheet &#8211; it is the value of the F2 cell:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4.png\"><img decoding=\"async\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-1024x86.png\" alt=\"\" class=\"wp-image-9158\" width=\"518\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-1024x86.png 1024w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-300x25.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-768x64.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-1536x129.png 1536w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4-500x42.png 500w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-4.png 1694w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>And how did we build this one?<\/p>\n\n\n\n<p>This is how:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><a href=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5.png\"><img decoding=\"async\" src=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5-1024x134.png\" alt=\"\" class=\"wp-image-9159\" width=\"518\" srcset=\"https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5-1024x134.png 1024w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5-300x39.png 300w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5-768x101.png 768w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5-500x66.png 500w, https:\/\/www.hexacorn.com\/blog\/wp-content\/uploads\/2024\/04\/x-5.png 1052w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>What we do here is building a basic ternary logic where we take the one part of the comparison from the current row, and then the alternative is taken from the row below. In the end, the sort-of recursion happens and we end up with a sequence of nested ternary operators doing its work.<\/p>\n\n\n\n<p>It may be a bit surprising, but using Excel for building complex logical statements like the one above, the one that in the end can be pasted directly to your favorite programming editor is actually very easy&#8230;<\/p>\n\n\n\n<p>You benefit from the fact the input data is saved in Excel format and is easy to edit, plus &#8211; as long as the formulas are correct &#8211; the resulting nested constructs are written (generated) in a syntactically correct way and with far less chances to introduce a basic typo error.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Excel is the emperor of automation. Not the SOAR type, but the local one &#8211; yours. Why? Its formulas and VBA capabilities can turn many awfully mundane tasks into plenty of automation opportunities&#8230; For instance&#8230; certain programming tasks. The case\/switch &hellip; <a href=\"https:\/\/www.hexacorn.com\/blog\/2024\/04\/25\/excelling-at-excel-part-4\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[105],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/9153"}],"collection":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/comments?post=9153"}],"version-history":[{"count":3,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/9153\/revisions"}],"predecessor-version":[{"id":9161,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/posts\/9153\/revisions\/9161"}],"wp:attachment":[{"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/media?parent=9153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/categories?post=9153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hexacorn.com\/blog\/wp-json\/wp\/v2\/tags?post=9153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}