Access Vlookup (or Dlookup) between two dates

BigNate

Board Regular
Joined
Dec 17, 2014
Messages
242
Hello Everyone,

I have a table called ListOfGroupNumbers. This table has three columns-- StartDate, EndDate, and GroupNumber. I want to use this table as a reference table to return the group number that an entry date belongs to (falls within the date range). It looks like this:

StartDateEndDateGroupNumber
1/7/20133/31/2013Group 1
4/1/20135/31/2013Group 2
6/1/20136/30/2013Group 3

<tbody>
</tbody>


I have another table called "ExcelData". This table has a field called EntryDate. I want to use the EntryDate as a lookup value, and figure out which Group number that belongs to (which date range that entry date falls within), based on the table "ListOfGroupNumbers" (shown above). For example, I want to write an expression in a query so that if I have an entry date of 4/7/2013, it will return "Group 2"

It's tough because I don't know if I can get a Dlookup to work due to the fact I'm dealing with a date range rather than just a single date.

Thank you in advance for your time and effort.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Code:
=DLookup("[GroupNumber]","
[ListOfGroupNumbers]","[StartDate] <= " & [EntryDate] & " And [EndDate] >= " & [EntryDate])

...is broadly how you would do it with a DLookup function

Are you doing this through VBA? If so you might need to assign your EntryDate to a date variable and pass that to the function (remember to format the date as mm/dd/yyyy and bookend it with "#")
 
Upvote 0
When getting/comparing data from more than one table, there should be a realtionship between at least each pair of them, otherwise you have to resort to hacks which don't always work reliably. DLookup can only return ONE value from any field, so if there is more than one that falls between your two dates, don't expect it to work unless you don't care which record is returned.
4/7/2013, it will return "Group 2"
I don't see why your query would return this for 04/07/2013, but I do see why if you said 04/01/2013, so that's confusing.
If you meant 04/01, then I sense you're saying the dates in your Excel table are related to the dates in ListOfGroupNumbers. If that's correct (i.e. any date in the latter would return the correct groupNumber using the same date from ListOfGroupNumbers, then just join the two tables by date in a query.

NOTE: if your date fields contain a time component (assuming they are correctly set to the date/time data type), you have other issues that will prevent this from working. Your BETWEEN statement requires special handling for date fields that contain time with respect to the end date. In those cases, the end of the end date is at the zero hour and you will get nothing with that days date if it is after (say 1:00 AM).
 
Last edited:
Upvote 0
Thank you both for your knowledge. I'm going to try a few things and see what happens.
 
Upvote 0

Forum statistics

Threads
1,221,847
Messages
6,162,380
Members
451,760
Latest member
samue Thon Ajaladin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top