Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Assigning a Macro to a Keyboard Combination
Hiding Rows Based on a Cell Value
David asked how he could return a date associated with the maximum value in a particular column. In David's application, he had two columns containing dates and weights associated with those dates. He could figure out how to determine the maximum weight, but not how to pull the date on which that weight occurred.
In this instance, let's assume that the dates are in column A and the weights are in column B, rows 2 through 45. The following is the method of determining the maximum weight in column B:
=MAX(B2:B45)
You are not limited to using the MAX function; you can also use the LARGE function, which returns the largest specified value in a range. If you want the largest value, you use the function in this way:
=LARGE(B1:B45,1)
So far so good. In order to pull the corresponding date from the cell to the left of the date, all you need to do is use one of the lookup functions. The following example uses the INDEX function:
=INDEX(A2:A45,MATCH(MAX(B2:B45),B2:B45,FALSE),1)
The INDEX function pulls the value from row n, column 1 of range A2:A45. The value of n is determined by using MATCH to get the position number of the maximum value of range B2:B45 within that range. For example, if the highest weight is the fourth one on the list (the weight in row 5), the MATCH expression returns 4, therefore the INDEX function returns the date from row 5, column 1 of range A2:A45.
If you wanted, you could simplify the formula even more by just specifying column information within it. In this way, you could have as many entries as desired in columns A and B:
=INDEX(A:A,MATCH(MAX(B:B),B:B,FALSE),1)
You could just as easily use the LARGE function in place of the MAX function in these formulas. If there are two dates that have the same weight associated with them, then only the first matching date is returned. In addition, you will need to format whatever cell contains your formula with a date format.
If your columns are reversed, meaning that the weights are in column A and the dates are in column B, then you would use the VLOOKUP function instead of INDEX, as shown here:
=VLOOKUP(LARGE(A1:A45,1),A1:B45,2,FALSE)
Again, the MAX function could be used in this formula instead of the LARGE function, if desired.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1972) applies to Microsoft Excel versions: 97 2000 2002 2003 2007
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.