Check date is is a range and return value of period

JonRowland

Active Member
Joined
May 9, 2003
Messages
415
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would have previously done this use and IF and AND function as only had 2 or 3 periods to check but I now have a lot. So here is my question.
I have a long list and what to find what period the date falls in against in a list containing Start and End dates and then return the value if date falls in that range.

Worksheet One
I have in Col U a date as dd/mm/yyyy hh:mm:ss. I want to check this against worksheet 2 and if falls in the date range return the value in Col C, similar to a X or V LOOKUP

So if U2 is greater or equal to Worksheet2.A1 AND less than or equal to Worksheet 2.B1 value WORKSHEET2.C1 is returned

For example
01:01:23 01/02/2022P1
23:59:01 02/01/2022No
11:11:01 07/01/2022P2


Worksheet Two
00:01:00 01/02/202223:59:00 02/01/2022P1
23:10:40 06/01/202211:11:01 07/01/2022P2
13:22:01 08/02/202216:00 09/02/2022P3

All ranges are dynamic.

I'd prefer a formula rather than VBA but not discounting VBA. I did think I could write a loop but would have to run through each series individually (I think).

Any guidance as always greatly received and hope I'm understandable.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I think you can achieve this with XLOOKUP in office 365 but I am not familiar with it. Here is the old school method:

Excel Formula:
=INDEX(Sheet2!$C:$C,MATCH(TRUE,(Sheet2!$A:$A<=$U2)*(Sheet2!$B:$B>=$U2),0))
 
Upvote 0
I think you can achieve this with XLOOKUP in office 365 but I am not familiar with it. Here is the old school method:

Excel Formula:
=INDEX(Sheet2!$C:$C,MATCH(TRUE,(Sheet2!$A:$A<=$U2)*(Sheet2!$B:$B>=$U2),0))

This ends up giving me a #N/A value when it should return a value. Stump as never been able to master Index/Match
 
Upvote 0
Try:
Excel Formula:
=INDEX(Sheet2!$C:$C,MATCH(1,(Sheet2!$A:$A<=$U2)*(Sheet2!$B:$B>=$U2),0))

That works. Fantastic. Thank you Flashbond.

Strangely enough, I decided to write some rushed VBA. Good thing is that each method returned the same results. Needs a little improving but does the job. [I'm in between PCs as my current one is slowly dying on me hence the rush before another BSOD)

VBA Code:
Sub BtwDates()

Dim Row As Long
Dim Trow As Long

Dim StartRow As Long
Dim LastRow As Long

Dim TStartRow As Long
Dim TLastRow As Long

Dim DateVlu As Date
Dim StPeriod As Date
Dim EndPeriod As Date
Dim DatePeriord As String

Call TurnoffAlerts ' My proc for turning of screen updating etc

StartRow = 2
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

' T is the date period list
TStartRow = 2
TLastRow = ActiveWorkbook.Sheets("Trips").Cells(Rows.Count, "A").End(xlUp).Row

' Loop within a loop.
For Trow = 2 To TLastRow

' Places the values of Start & End Date and the Period referred into variables 
StPeriod = ActiveWorkbook.Sheets("Sheet1").Cells(Trow,1)
EndPeriod = ActiveWorkbook.Sheets("Sheet2").Cells(Trow,2)
DatePeriod = ActiveWorkbook.Sheets("Sheet2").Cells(Trow, 3)

  For Row = StartRow To LastRow

DateVlu = Cells(Row, 13) ' When date stored

If DateVlu >= StPeriod And DateVlu <= EndPeriod Then

Cells(Row, 12) = DatePeriod 'Update main WkSht with DatePeriod Value
End If
    Next Row
    
    Next Trow
    
    Call TurnonAlerts ' Turn updating back on
End Sub
 
Upvote 0
My VBA approach would be:
VBA Code:
Sub myFunction()
  Dim s1lRow As Long, s2lRow As Long
  Dim sh1 As Worksheet
 
  Set sh1 = Worksheets("Sheet1")
  s1lRow = sh1.Cells(Rows.Count, 21).End(xlUp).Row
  s2lRow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

  Call TurnoffAlerts
  With Worksheets("Sheet2")
    For j = 2 To  s1lRow
      For i = 2 To s2lRow
        If sh1.Cells(j, 21).Value >= .Cells(i, 1).Value And sh1.Cells(j, 21).Value <= .Cells(i, 2).Value Then
          sh1.Cells(j, 22).Value = .Cells(i, 3).Value
        End If
      Next
    Next
  End With
  Call TurnonAlerts
End Sub
 
Upvote 0
Thanks Flashbond - looks neater than mine. I will give this a try once I am properly up and running again.
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,405
Members
448,958
Latest member
Hat4Life

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