Solved

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

Posted on 2006-11-27
7
5,343 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will show you how to use shortcut menus in the Access run-time environment.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

816 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

8 Experts available now in Live!

Get 1:1 Help Now