Solved

excel--find last occurrence of a value in a range.

Posted on 2006-11-27
7
5,242 Views
Last Modified: 2011-08-18
How would I find the last occurrence of a non-unique value in a range. Although LAST isn't a function, but something like this for example: IF(LAST(value,range)*TRUE,"last occurrence","").
Thank you
0
Comment
Question by:cwest1
  • 5
  • 2
7 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 18024977
Hi cwest1,
If you want to find the index number of the last occurrence of a value in a range, then try an array formula like:
=IF(COUNTIF(I4:I11,"b")=0,"",MAX(IF(I4:I11="b",ROW(I4:I11)-ROW(I4)+1,"")))
Array formulas need to be entered in a special way: hold the Control and Shift keys down, then press Enter. Excel should respond by adding curly braces { } surrounding the formula. If it doesn't, then select the cell, click in the formula bar and CTRL + Shift + Enter.

In this formula, the range being searched is I4:I11 and the value being sought is "b". If the value sought isn't present, the formula returns an empty string.

Hoping to be helpful,

Brad
0
 
LVL 81

Expert Comment

by:byundt
ID: 18024983
cwest1,
If you don't want to find an index number, could you please post a simple example and show the expected result?
Brad
0
 
LVL 81

Expert Comment

by:byundt
ID: 18025169
cwest1,
If you want to find the last value in a range, then consider using the LOOKUP function. It returns the last value if it cannot find a match.
=LOOKUP("zzzzz",I4:I11)            finds the last text value in the range I4:I11. The string zzzzz is sure to be last in an alphabetical sort of the range.
=LOOKUP(1E40,I4:I11)                finds the last numeric value in the range I4:I11. The value 1E40 is a number so large it won't be found in the range.
Brad
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:cwest1
ID: 18025325
Thanks Brad. Got me going in right direction :)

The spreadsheet I'm working with contains dates in column A and customer numbers in column I. The same customer number occurs several times in column I. I want to get the latest date a customer number occurred in column I. Column T contains unique customer numbers. The following appears to work. Appreciate any comments.

{=IF(T2<>"",INDEX($A$2:$I$744,MAX(IF($I$2:$I$744=T2,ROW($I$2:$I$744),"")),1),"")}
0
 
LVL 81

Accepted Solution

by:
byundt earned 250 total points
ID: 18025483
In my original formula, I used the COUNTIF function to make sure that the value being sought actually occurs in the range being searched. You know your data better--is it sufficient to test whether T2="" ?

You are basing your INDEX on A2:I744 -- but using the ROW function without subtracting 1 (ROW returns the series 2 through 744, but you need 1 through 743 for the INDEX).

You are also returning a value from column A, so the INDEX function doesn't need to refer to column I. The only reason for including more columns in the INDEX range would be if you are planning to change the column index 1 as you copy the formula across. If you restrict the INDEX range to column A, then it won't recalc if there is a change to columns B through I -- a potential improvement in workbook recalc time.

Here is how I think your formula should be written:
{=IF(T2="","", INDEX($A$2:$A$744,MAX(IF($I$2:$I$744=T2,ROW($I$2:$I$744)-1,"")),1))}

Brad
0
 

Author Comment

by:cwest1
ID: 18025507
Thanks Brad. Much appreciated.
0
 
LVL 81

Expert Comment

by:byundt
ID: 18025512
cwest1,
Thanks for the grade!
Brad
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now