Results 1 to 10 of 542

Thread: Appendix Thread. App Index Rws() Clms() Majic code line Codings for other Threads, Tables etc.)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    testies

    LOOKUP function and finding the last filled cell in a column


    This is probably not worthy of a Tips, tricks or tutorials post, as it is a simple Excel standard function and there are almost certainly a lot more better Blogs, Videos etc. on it. I am also not initially looking at it extensively
    But I needed it and a specific typical use of it, and it was not known to me, and so I made my own notes, and excelfox.com is a good place to access my stuff, and also the use of it was in ( another more substantial Blog posting, ) , where I came up with some similar formulas with VLOOKUP in a frenzy on Panzerschokolade, so can’t remember how I got it…..
    So all in all it seemed OK to drop it in here, as I may need it to refer to later, possibly when others catch up with me, when/have I stop/ stopped taking those drugs.

    ( I actually stated using any of the Excel Loolup things , with VLOOKUP and had missed noticing this more fundamental LOOKUP. In the meantime there is the newer XLOOKUP, or some such, which like any new function excites the Microsoft loyal Excel addicts like excited children with a new toy.
    But I am keen to encourage using just the basic fundamental functions as much as possible, ( and then thereafter, rather moving on to VBA / user defined functions for more complex things, so that gives me yet another reason for me posting it here
    . )





    This first post will not be an extensive explanation of LOOKUP. It will just be enough to get me as far as moving on in the next post to explain a typically used formula / function version used to find the last filled cell in a column in a spreadsheet


    Simplest 2 argument LOOKUP
    For its simplest spreadsheet use we need a single row or column of numerical values, and for a simple explanation of its behaviour , this should be ascending in value, so something like this
    1
    3
    5
    6
    8
    If I apply the simplest version of LOOKUP to that example, and say I am looking in that column for a number of 4.9, then the function as written might look something like this, (assuming in this example I have the number 1 in cell E15, and am using the range E15:E19 for all of the 5 numbers )
    =LOOKUP(4.9,E15:E19)
    The final solution forms I am envisaging, are likely to involve array type things, so therefore as I proceed from here further, I will try to always show an array alternative, such as this alternative, using hard coded numbers rather than a spreadsheet range, as it will work on an array instead.
    =LOOKUP(4.9,{1;3;5;6;8})
    ( , most spreadsheet formulas will work in some way or another with arrays, at least almost always in a spreadsheet.
    Since Excel tends to update the whole spreadsheet, or at least a large part of it, it is often efficient to take advantage of this, which is what the array calculation concept in Excel is based on )

    This would be how the test range and the LOOKUP formulas looks in a spreadsheet
    Row\Col E F
    15 1 =LOOKUP(4.9,E15:E19)
    16 3 =LOOKUP(4.9,{1;3;5;6;8})
    17 5
    18 6
    19 8



    In both cases the answer you would see would be 3, like this:

    Row\Col E F
    15 1 3
    16 3 3
    17 5
    18 6
    19 8


    Perhaps it is not obvious why we get 3 when looking for 4.9 ?

    To Explain:
    What seems to be happening is that it assumes the numbers go up in value as you go along/down ( ascending ), (as we have in this example), and it will either give back the value if it finds it, or otherwise it gives you the next value down.
    So it is looking for the nearest match, … err .. or .. ? - .. well not quite… it is looking for the value, (and if it finds it , then it will return that number), but if it does not find it, it gives you the nearest value next one down.
    Another way of looking at this is to say we get a value out for a certain range of numbers we might give in the first argument, and this returned number is the lowest number in that certain range, like A to C goes in tray A, D to F goes in try D, F to W goes in tray F etc…
    In the example above, something approximately as in this sketch form
    Spread of numbers
    We might give
    In the first argument
    ----- value returned by VLOOKUP ( tray identificater )
    ___1 - 2.999 --------------- 1
    ___3 – 4.999 --------------- 3
    ___5 – 5.9999 -------------- 5
    ___6 - 7.999 --------------- 6
    ( If you give in this example anything above 8 in the first argument in the LOOKUP function , then the LOOKUP function will return 8 )
    ____> 8 …….. --------------- 8




    3 argument LOOKUP
    In the previous example the "nearest", (nearest lowest), value in the second argument column range, that nearest to the first argument value you gave, was the output. (Or indeed the exact value you gave , if that value was in the list).
    By adding a new third argument as a similar sized range, the output will then be the value at the corresponding row/position along in that new third argument range. This new extra range can be a range of numbers or text or both.

    So the first column range (second argument) is used to find the "nearest" row/position along that the number you give as first argument "belongs" to , (In other words the number range the number you give as first argument is in, as per the sketch above), but the output now LOOKUP gives is the value in the corresponding row in the second range column, (third argument).
    So effectively you can determine what output is given for a range of numbers given in the first argument.


    For example it can be used to give an output to tell you in which number range your number was. Extending/ following on from the last example…..
    Row\Col E F G H
    15 1 3 |1--3| |3--5|
    16 3 3 |3--5| |3--5|
    17 5 |5--6|
    18 6 |6--8|
    19 8 | < 8 |

    Row\Col E F G H
    15 1 =LOOKUP(4.9,E15:E19) |1--3| =LOOKUP(4.9,E15:E19,G15:G19)
    16 3 =LOOKUP(4.9,{1;3;5;6;8}) |3--5| =LOOKUP(4.9,{1;3;5;6;8},{"|1--3|";"|3--5|";"|5--6|";"|6--8|";"| < 8 |"})
    17 5 |5--6|
    18 6 |6--8|
    19 8 | < 8 |


    I will leave the explanation of the function at this stage, possibly me or someone may come back in further posts after the next ones, later, or probably much later, if at all..
    For now I want to concentrate on a particular example.


    Last edited by DocAElstein; 09-11-2025 at 12:24 AM.

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Alternative Codes using [ ] shorthand

    Using LOOKUP to get the last value

    For the particular formula that I am trying to get to and understand, ( one of this sort of form =LOOKUP(2;1/(C:C<>"");C:C) ) ,I need to look a bit further into the behaviour of LOOKUP, in particular the second argument.

    A few observations:
    _observation
    VLOOKUP seems to assume that the list/ range of numbers is in ascending order
    For example, this sort of thing,
    1
    3
    4
    2
    Row\Col E
    37 1
    38 3
    39 4
    40 2
    41
    42

    , with the LOOKUP formula of these sort of things
    =LOOKUP(5;E37:E40)
    =LOOKUP(5;{1;3;4;2})

    , will return 2. In fact it always seems to return the last value. This initially points us into perhaps a direction of thinking to just look for any number likely to be bigger than any in the list of numbers (in the second argument range)

    _observation
    Extend the range a bit , to example include some empty cells,
    =LOOKUP(5;E37:E43)
    , and once again the answer is 2.
    This is very interesting and helpful. It seems not to error when not getting numbers, (as I was thinking that the second argument would be always expecting numbers). The empty cells simply seem to be ignored

    But at this point some alarm bells are ringing in my head. The final solution forms I am envisaging, are likely to involve array type things, so I want to continue as I have done so far, to consider the array alternative, and I am getting worried now – because…
    depending on your viewpoint, an empty cell can be regarded and one if at least 3 things I know about
    ___ Empty
    ___ 0
    ___ a zero length string, often represented by ""

    Well, I can’t be too sure about how to distinguish all 3 in a formula, but I can do a partial job by considering these two array formula versions
    =LOOKUP(5;{3;4;2;0;0;0}) ' returns 0
    =LOOKUP(5;{3;4;2;"";"";""}) ' returns 2


    The difference in those two results is enough for me to ponder about for a while. But I don’t need tom ponder too long.
    It makes sense where we get 0, since 0 is a number, and in line with what we have already said
    _____ VLOOKUP takes the very last number in a list if it never finds either the number it is looking for , or if it never finds a larger number at any other position in a list, (in this latter case it would take the number before that bigger number)
    For the purposes of the array type formula, "" , can be regarded, (with some caution perhaps), of behaving like our spreadsheet empty cell. We will come back to this last point, but let’s give a pictorial summary so far of where we are

    Row\Col E F
    37 1 =LOOKUP(5,E37:E40)
    38 3 =LOOKUP(5,{1;3;4;2})
    39 4 =LOOKUP(5,E37:E43)
    40 2 =LOOKUP(5,{3;4;2;0;0;0})
    41 =LOOKUP(5,{3;4;2;"";"";""})
    42
    43

    Row\Col E F
    37 1 2
    38 3 2
    39 4 2
    40 2 0
    41 2
    42
    43





    In Excel functions and formulas generally, the difference between using a number and a text can be significant. The fact that the array version both accepts ( does not error with ) the "" , and gives us perhaps a useful answer , the last number, could be useful….

    Also it may be worth checking what happens if the text is not zero length.
    Here we go, lets try filling the empty cells with some text:
    Row\Col E F
    37 1 =LOOKUP(5,E37:E41)
    38 3 =LOOKUP(5,{1;3;4;2})
    39 4 =LOOKUP(5,E37:E43)
    40 2 =LOOKUP(5,{1;3;4;2;0;0;0})
    41 a =LOOKUP(5,{1;3;4;2;"a";"b";"c"})
    42 b
    43 c

    Row\Col E F
    37 1 2
    38 3 2
    39 4 2
    40 2 0
    41 a 2
    42 b
    43 c

    Hmm…. So it seems to treat an actual text just like the zero length text



    In the next post we will develop/ explain a solution
    Last edited by DocAElstein; 09-10-2025 at 12:36 AM.

Similar Threads

  1. Replies: 192
    Last Post: 08-30-2025, 01:34 AM
  2. Replies: 3
    Last Post: 03-07-2022, 05:12 AM
  3. HTML (Again!) arrOut()=Index(arrIn(),Rws(),Clms()
    By DocAElstein in forum Test Area
    Replies: 1
    Last Post: 08-23-2014, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •