简体   繁体   中英

Excel formula to return last xth numeric value from row

I have a data set like this:

a   1       2   3       5       6   7
b       5       1   3   4           
c   7   4       3   2       6       
d   1   2   3                       7
e   7   5               5   6   7   

which i want to transform in excel to only show the last 4 numeric values for each row, ie

a   3   5   6   7
b   5   1   3   4
c   4   3   2   6
d   1   2   3   7
e   5   5   6   7

Ive managed to use =INDEX(row1,MATCH(9.99999999999999E+307,row1)) which correctly returns the last value (ie 7 for row1 in this case) but how could i get the 2nd last, 3rd last, 4th last etc?

With a in A1, in M1 copied across and down to suit:

=MID(RIGHT($B1&$C1&$D1&$E1&$F1&$G1&$H1&$I1&$J1,4),COLUMN()-12,1)

then hide ColumnsB:L.

(Only works for single digits, as shown. Recent versions of Excel may offer better options.)

So I managed to figure out how to do this generically for any digits:

Say your original numerical data is in cells D5:N9 .

OPTIONAL:
If any of the rows of data contain the same value twice create a copy in cells D13:N17 which amends duplicates slightly by using in D13 and dragging to fill
=IF(D5="","",D5+0.0001*(COUNTIF($D5:D5,D5)-1))

Then in cells A13:A17 enter values 13 , 14 , 15 , 16 , 17
and in cells A20:A29 enter values D , E , F , G , H , I , J , K , L , M

In Cell D20 you can return the last from row 13 value using:
=LOOKUP(9.99999999E+307,D13:N13)
and copy this down for each row.

In Cell E20 you can return the 2nd last entry using =LOOKUP(9.99999999E+307,$D13:INDIRECT(CONCATENATE(INDEX($A$20:$A$29,MATCH(D20,$D13:$N13,0)-1),$A13)))
and copy this down for each row and drag it across to H24 to fill in all the values for 3rd last, 4th last and 5th last for each row.

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