{"id":15161,"date":"2018-05-08T16:04:48","date_gmt":"2018-05-08T23:04:48","guid":{"rendered":"https:\/\/www.podfeet.com\/blog\/?p=15161"},"modified":"2018-05-10T13:43:33","modified_gmt":"2018-05-10T20:43:33","slug":"how-to-find-the-cell-reference-for-a-searched-value-in-an-array-in-excel","status":"publish","type":"post","link":"https:\/\/www.podfeet.com\/blog\/2018\/05\/how-to-find-the-cell-reference-for-a-searched-value-in-an-array-in-excel\/","title":{"rendered":"How to Find the Cell Reference for a Searched Value in an Array in Excel"},"content":{"rendered":"<p><a href=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo-200x200.png\" alt=\"excel logo\" width=\"200\" height=\"200\" class=\"alignright size-thumbnail wp-image-15174\" srcset=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo-200x200.png 200w, https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo-300x300.png 300w, https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo-768x768.png 768w, https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo-650x650.png 650w, https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/excel-logo.png 1024w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/><\/a>John Ormsby (aka NASANUT) sent in what he playfully called a Dumb Question:<\/p>\n<blockquote><p>I have a spreadsheet that contains a 10 by 10 matrix of values. Each value is different. There are column headings at the top, ranging from 0 to 9 and there are row headings on the left, also ranging from 0 to 9.<\/p>\n<p>What I want to do is this. I want to search for a value within the matrix, and if found, want Excel to tell me the row and column that it is in. In other words, if I search the matrix for \u2018150\u2019 and it is found at cell F7, then Excel can either tell me \u2018F7\u2019 or the row and column headings (I can work with either).<\/p>\n<p>The problem I have is that all of my searching only results in information on how to go the other way (i.e. provide the column and row and \u2018find\u2019 the value).\n<\/p><\/blockquote>\n<p>The beautiful thing about this question is that a) it&#8217;s not at all dumb, it&#8217;s a surprisingly hard problem, and b) I had a lot of other things on my to do list (some of them fairly urgent) but of course I dropped everything to try to figure this out!<\/p>\n<p><!--more-->When I first went searching for a way to return the cell reference (aka F7), I found a function called ADDRESS. At first blush it appears to be exactly what we want. If we enter the formula:<\/p>\n<p><code>=ADDRESS(cellRow,cellColumn)<\/code><\/p>\n<p>Using 7,6 as the row and column, it will indeed return F7.  That&#8217;s really swell, but how do we figure out what the row and column numbers are?  That&#8217;s when things got seriously tricky.<\/p>\n<p>Mike Rickson in the <a href=\"https:\/\/www.mrexcel.com\/forum\/excel-questions\/516742-how-return-results-position-2d-array.html\" target=\"_blank\" rel=\"noopener\">mrexcel.com forum<\/a> gave me the basic idea of how to attack the problem. I changed a bit of what he did to suit our specific need.  <\/p>\n<p>I&#8217;m going to step through all of this breaking down each step, but <strong>at the end you will only need one elegant but long command<\/strong> to perform this entire function.  At the very end of this explanation is an Excel file you can download so you can play with it yourself to see how it works. Ok, ready for some geekery?<\/p>\n<h4>Unary Operator<\/h4>\n<p>Mike&#8217;s solution makes use of something called a double unary operator. Bart taught us about unary operators in JavaScript but it seems to work differently in Excel.  In this instance, we&#8217;ll be using a minus symbol as a unary operator.<\/p>\n<p>If you have a positive 6 in cell B7 and you create a formula that says<br \/>\n<code>=-B7<\/code> <\/p>\n<p>it will return -6.<\/p>\n<p>If you have a negative 6 in cell B7, it will return 6. It&#8217;s simply switching the sign.<\/p>\n<p>What if you use the negative\/hyphen\/minus\/unary operator twice? With regular numbers, the two cancel each other out, so &#8212; applied to 6 gives you back 6, and &#8212; applied to -6 returns -6.<\/p>\n<p>So far, that&#8217;s pretty obvious.  But what happens if you apply the unary operator to TRUE\/FALSE values?  By some mysterious convention decided on a long time ago, &#8211; applied to TRUE coerces the value to -1, and &#8211; applied to FALSE coerces it&#8217;s value to 0.  That&#8217;s the actual term, &#8220;coerce&#8221;.<\/p>\n<p>If you use the negative\/hyphen\/minus\/unary operator twice, it first coerces the TRUE\/FALSE to -1\/0, and the second &#8211; just flips the sign like any other number.  So &#8211; applied to TRUE coerces to -1, and the second &#8211; flips the sign so you get positive 1.  Nothing interesting happens to FALSE when you apply the unary operator twice though, since it&#8217;s already 0.<\/p>\n<p>At the risk of saying two wrongs made a right, is everyone still with me?<\/p>\n<h4>Using the Unary Operator to Make an Array<\/h4>\n<p>We&#8217;re going to run some functions to search for our value in the array. Normally this would return an array of TRUE\/FALSE values.  But with our handy unary operator, we can return a new array that has all 0s, except where it finds our value and in that position it will place a 1.  <\/p>\n<p>So why does THAT help us at all? We need a second array before it will all become clear.  Our next array makes use of the ROW function.  We point the ROW function at the original array and it returns an array where every row simply repeats the row number. It will have 1,1,1,1 in the first row, 2,2,2,2, in the second row.  <\/p>\n<p>Note that the ROW function returns the absolute row reference, not the row within your range.<\/p>\n<p>Now, armed with our two arrays, one with all 0s except a 1 where the searched value was, and one with the row numbers, we can finally get something done.  If we multiply the two arrays against each other, all the 0s will stay 0s, but the 1 will turn into the row number.  We now know the row number of our searched value.  But it&#8217;s still buried in an array.<\/p>\n<p>An array with that one cell showing us the row value isn&#8217;t that helpful, we need it all by itself.  Instead of multiplying our two matrices together, if we do a SUMPRODUCT of the two matrices, it will drop out a single value, the row number of our found value!  If we repeat using COLUMN instead of ROW, we will have both values to plop into our original ADDRESS function to find the cell reference of the found value (which was our original objective).<\/p>\n<p>Let\u2019s walk through an example.<\/p>\n<p>We will name the range of values we\u2019re searching to be called dataRange. If this is our dataRange:<\/p>\n<table border=\"0\" style=\"width:100px\">\n<tr>\n<td>11<\/td>\n<td>22<\/td>\n<td>33<\/td>\n<\/tr>\n<tr>\n<td>44<\/td>\n<td>55<\/td>\n<td>66<\/td>\n<\/tr>\n<tr>\n<td>77<\/td>\n<td>88<\/td>\n<td>99<\/td>\n<\/tr>\n<\/table>\n<p>First, select an empty range of cells in your spreadsheet that&#8217;s the same size as dataRange (same number of rows and columns).  With that new range of cells selected, if we want to find the location of the value 66 enter the following:<\/p>\n<p><code>--(dataRange=66)<\/code> <\/p>\n<p>And hit Control-Shift-Enter.  When you hit CSE (as it&#8217;s referred to by all the cool kids), the formula will now have braces around it, showing that it&#8217;s an &#8220;array function&#8221;.<\/p>\n<p><code>{--(dataRange=66)}<\/code><\/p>\n<p>You now should see the following in your new array::<\/p>\n<table border=\"0\" style=\"width:100px\">\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<\/table>\n<p>Select another empty region the same size as our original and enter this formula (followed by CSE):<\/p>\n<p><code>={ROW(dataRange)}<\/code> <\/p>\n<p>And assuming our original array is in the top left corner, it will return the array:<\/p>\n<table border=\"0\" style=\"width:100px\">\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>2<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3<\/td>\n<td>3<\/td>\n<\/tr>\n<\/table>\n<p>Now if we multiply these two arrays together, again selecting a new range of cells the same size as our original:<\/p>\n<p><code>={--(dataRange=66)*ROW(dataRange)}<\/code> <\/p>\n<p>we would get the array:<\/p>\n<table border=\"0\" style=\"width:100px\">\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<\/table>\n<p>Instead, let&#8217;s do the SUMPRODUCT of the two arrays (which does not create a new array and so does not require selection of an empty region nor does it require CSE.<\/p>\n<p><code>={SUMPRODUCT(--(dataRange=66)*ROW(dataRange))}<\/code> <\/p>\n<p>returns the value of 2.<\/p>\n<p>We now know that 66 is in row 2 of our range. <\/p>\n<p>Running this again only with COLUMN gives us column 3 for our found value:<\/p>\n<p><code>={SUMPRODUCT(--(dataRange=66)*COLUMN(dataRange))<\/code> <\/p>\n<p>returns the value of 3 is the column number.<\/p>\n<p>Now with our amazing knowledge that 66 is in row 2, column 3, we can finally convert that into the cell reference designation by entering those two values into the ADDRESS function.<\/p>\n<p><code>=ADDRESS(2,3)<\/code> <\/p>\n<p>returns $C$2.  <\/p>\n<p>Note that the dollar symbol in front both the column and row designations means this is the absolute location.  However, if you select your dataRange and move it to another location in the sheet, the cell reference designation returned by the formula will still update to the new information.<\/p>\n<p>We can combine all of these formula bits and pieces together into one giant formula of doom (&#x2122;Donald Burr). Note that I\u2019ve created another variable called myValue which is the value for which we\u2019re searching.<\/p>\n<h4>The One, Long, Elegant Formula!<\/h4>\n<p><code>=ADDRESS(SUMPRODUCT(--(datarange=myValue)*ROW(datarange)),SUMPRODUCT(--(datarange=myValue)*COLUMN(datarange)))<\/code><\/p>\n<p>I want to add one caveat to this entire exercise, this works only if the values\u00a0in the array are unique. Otherwise, it barfs out a nonsense location not even IN the array!  I&#8217;m betting someone clever will make a comment below explaining how to have it error out in a more sensible way.<\/p>\n<p>If you&#8217;d like to download an example spreadsheet where I break it down step by step. You can download it from my OneDrive account here: <a href=\"https:\/\/1drv.ms\/f\/s!Ahm2TQrc8G9UiFv_XG3HXbUDyAzp\" target=\"_blank\" rel=\"noopener\">1drv.ms\/&#8230;<\/a><\/p>\n<p>Here&#8217;s a screenshot of the downloadable spreadsheet:<\/p>\n<p><a href=\"https:\/\/1drv.ms\/f\/s!Ahm2TQrc8G9UiFv_XG3HXbUDyAzp\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" style=\"display:block; margin-left:auto; margin-right:auto;\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/05\/cell-reference-calculations-broken-down.png\" alt=\"Cell reference calculations broken down\" title=\"cell reference calculations broken down.png\" border=\"0\" width=\"983\" height=\"814\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>John Ormsby (aka NASANUT) sent in what he playfully called a Dumb Question: I have a spreadsheet that contains a 10 by 10 matrix of values. Each value is different. There are column headings at the top, ranging from 0 to 9 and there are row headings on the left, also ranging from 0 to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":14855,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[147,148],"tags":[2523,250,249,1897,2524,2521,2522],"class_list":["post-15161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-posts","category-dumb-question-corner","tag-cell-reference","tag-dumb-question","tag-dumb-question-corner","tag-excel","tag-mrexcel","tag-unary","tag-unary-operator"],"jetpack_featured_media_url":"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2018\/04\/dumb-question-corner-800px.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/15161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/comments?post=15161"}],"version-history":[{"count":16,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/15161\/revisions"}],"predecessor-version":[{"id":15184,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/15161\/revisions\/15184"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media\/14855"}],"wp:attachment":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media?parent=15161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/categories?post=15161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/tags?post=15161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}