Home » excel » excel – Lookup by Range

excel – Lookup by Range

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have the following table:

enter image description here

I need to fill the Group column with A, B, … using the age of the person and matching to Age Ranges.

So John, with age 5, would get the Group A. I was looking at the VLookup and HLookup functions.

The problem is that I am not sure if this is possible because of the Age Ranges: 0-7, 8-14, etc.

Do I need VBA for this?

How to&Answers:

Yes, VLOOKUP is the way to go,and there is no need for VBA. Here’s the setup:

.enter image description here

Couple of things to notice.

  • The break points for the lookup are the lower bound of each group range

  • the lookup range itself is sorted

  • the third argument of the lookup is TRUE (the default) which means the function does a range rather than an exact lookup.

Answer:

I prefer @chuff’s answer but since this is SO you could use code such as:-

Sub Macro1()
Range("C2").Select
ActiveCell.FormulaR1C1 = _
    "=IF(RC[-1]>60,""E"",IF(RC[-1]>18,""D"",IF(RC[-1]>14,""C"",IF(RC[-1]>7,""B"",""A""))))"
Selection.Copy
Range("B2").Select
Range(ActiveCell, ActiveCell.End(xlDown)).Offset(0, 1).Select
ActiveSheet.Paste
End Sub  

assuming you want A for John in C2.