Originally Posted by
jbop
Why did you use REPT Z 255 times?
REPT("Z",255) as you infer creates a string of Z repeated 255 times which is as you can imagine a pretty "big" string... given "ab" > "a" thus "zzzz..." > other strings...
Why use it ? Well LOOKUP assumes all values in the lookup_vector to be sorted in ascending order, so...
in the case of numbers:
1,2,3 etc
in the case of strings
a,ab,b etc...
the default function of LOOKUP is to find the greatest value in the lookup_vector which is <= criteria value.
Given the assumption of the values being in ascending order it will assume therefore that the last value in the lookup_vector is the greatest and thus if the criteria > last value it returns the last value... eg:
given the criteria ("d") is bigger than all values in the lookup_vector LOOKUP actually returns "a" given it assumes the list is to be sorted in alphabetical order and therefore deems the last value "a" to be the biggest value in the lookup_vector (in reality we know this is not the case) and given "a" < "d" returns "a" as this is seen as biggest value < d in the vector.
If we were to change the criteria from being bigger than all values to a value <= biggest vlaue we would get a different result (binary search algorithm employed)
So if we want to return the "last value" in the vector using LOOKUP it is essential that the criteria exceeds all known values... this is why we use REPT("Z",255) as the criteria when looking for last text string, ie
Originally Posted by
jbop
I don't understand the use of the choose function in this case either CHOOSE (1,2,3) .
We're using CHOOSE to create a 3 value lookup_vector ... where the first value is a Null (our default text string if both VLOOKUP fail to return a valid answer), the 2nd value is the 2nd VLOOKUP and the final value is the 1st VLOOKUP.
Note the ordering of the values in the lookup_vector, we're listing our possibel results in order of least preference.. ie the Null is the result we want as a last resort so to speak.
Given the use of CHOOSE to populate the lookup_vector we end up with
We know that from earlier section the above will return the last text string found... so if vlookup1 returns a valid name then that will be the result, if it doesn't and returns an error LOOKUP will ignore this and move to vlookup2.... and if that too fails to return a valid answer it will return the Null, to illustrate:
The "beauty" of LOOKUP is as you can see that it in fact ignores Error values when evaluating.
The same approach can be used to find "last number" in a range, replacing REPT("Z",255) with 9.99999999999999E+307 - see http://www.xldynamic.com/source/xld.LastValue.html
Bookmarks