简体   繁体   中英

Finding last value of row depending on the first column's variable in Excel

I'm currently trying to find a formula that will give me the last value of the row Total assets (94054000000) corresponding to the T1 2022. My main problem stems from the fact that I'm trying to automate this process and I don't want the formula to be based on the column's heading "T1 2022".

Is there a function that can just search through an array, find "total assets" as the value of the first column and give me the last value of the corresponding row ? Maybe this can be done through Vlookup , but I have not been able to find a way to accomplish what I want to do so far.

The two formulas that I've been using to try and extract the data have been.

=VLOOKUP("Total Assets",'10k B'!$A$2:$L$67,MATCH("T1 2022",'10k B'!$A$1:$L$1,0),0)

Is there a way to get the last value of the Total assets row without specifying that I want the column value to be T1 2022 ? The Total Assets row also move from one company to another, hence why I'd prefer a formula that can search through an array rather than just plainly doing something like

=LOOKUP(2,2/(2:2<>""),2:2)

在此处输入图像描述

Thank you in advance !

There are many ways. Different limitatiomns with each . So, wanted to try my own, make it sure.

Ok, a very simple, basic, low level of excel knowledge level, but it works for excel 2019+

4 ways (at least):

1.

So since all known methods (below and many more) to do it, have their small print expetions (Ie "won't give correct result if empty cells in range", "doesn't work if duplicates in row", and , "doest work when last cell is 0" - So you'd need accounting for - I have wanted to and decided to make a non-vba old fashioned Excel formula which will work to always give the last cell column number regardless. Here it is -

This approach assumes nothing. Doesn't need non-blank cells. (Ie works with even with blanks in your rows/ranges. Work even with duplicates & works even with 0 as last cell),Doesnt fail with 0's or empty cells, and gives you the position of last used cell in range/row without you having to know anything about your data, or the last cell value, whatever value it that is. Treats cells as blocks to work through & count from left to the last filled/last used cell .

[Depending on your excel version, you might need to split up formula by/into helper cells, or use a made-for-2016-earlier vba udf for the 360 concat function I'm using in here).]

Formula to find the position/last used cell in a row (without needing to fill blanks cells, or know its range/size, know if empty or non-empty cells or not, or needing to know anything at all about your data):

=IF(AND((LEN(A1))>0,ISERROR(LEN(LEFT(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),FIND("|"&"@"&"|",SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),1)))-LEN(SUBSTITUTE(LEFT(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),FIND("|"&"@"&"|",SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),1)),"|",""))+1)),1,LEN(LEFT(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),FIND("|"&"@"&"|",SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),1)))-LEN(SUBSTITUTE(LEFT(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),FIND("|"&"@"&"|",SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"|"&"@"&"|",(LEN(CONCAT(1:1&"|"))-LEN(SUBSTITUTE(CONCAT(1:1&"|"),"|"&LOOKUP(2,1/(1:1<>""),1:1),"")))/LEN("|"&LOOKUP(2,1/(1:1<>""),1:1))),1)),"|",""))+1)

paste in a cell you want the output, and it returns the last used column in your range.

Im sure someone somewhere could make/has a obvious better one, made with lambdas or textjoins i imagine, and im sure it does exists elsewhere (but tried exaustive looking for it and nothing on the web* it seems to not be done anywhere), so but happy ive done this now ( or you could just use vba ). Relieved I have done this.

You dont need to know anything about or do anything to your data (the value, the header, whats in your cells or if there are blank cells , unfilled cells in there or not, or whats in them). You don't need to know anything before hand. Thats what I made sure of. I've always wanted to do this with a only-formula after being told (and working on) the counta / countblank problem in earlier versions of excel.

The only thing you need to make sure of is the special characters used to make-it / push it to work, | and @ , are truelly unique and never present in your data, so perhaps use char(1) and char(200) or something partciular to you, to be sure.

There is also this,

2.

Index Match last value in range

But doesnt work when/if last value is duplicated.

=COLUMN(INDEX(1:1,MATCH(2,1/(1:1=LOOKUP(2,1/(1:1<>""),1:1)))))

This works well so for me too:

see this Display the column number of the last non-empty cell within a range (if values are not unique)

=AGGREGATE(14,7,(COLUMN(1:1)-MIN(COLUMN(1:1))+1)/(1:1>0),1)

But 'fails' if last value is 0 or 0's after yourdata (depending which way you see it)

4.

There is also another one off top of head cant remember it exactly, but its like a lookup with Mins and Sumproducts and a multiplacation in between, You may have come accross it by now. I was looking for that specific post where where I had detailed discussion/engagament a few year back, but I cant find it. Here is similar:

from Excel formula - last row dynamic range

=MAX((1:1=LOOKUP(2,1/(1:1<>""),1:1))*(COLUMN(1:1)))

Only doesnt work as expected when 0's after you data. You can get aroud that by wraping those 16384 type values with char(34) or similar.

So, knowing all the above, I decided/wanted to make number 1, because of the limitations of the above approaches (and others not mentioned) with "0's in or after your data", and/or "blank/non-blank cells" issues were annoying to me, even if they too have their uses.

So wanted a get/find last used cell whatever it is, however your data is arranged, and even when you don't even know it. Hence number 1.

This is how you can do it:

=INDEX(2:2,COUNTA(2:2))
  • 2:2 means row 2
  • COUNTA returns the number of nonempty cells in a row
  • INDEX returns the value

Note that the approach above assumes that all your cells up to the last one are nonblank.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM