If data is in between or equal to 2 dates, display a number

JennV

New Member
Joined
May 9, 2019
Messages
34
Sheet1:

Block 1Block 2Block 3
2019-05-202019-05-232019-05-27
2019-05-222019-05-262019-05-31

<tbody>
</tbody>

Sheet2:

24Cat2019-05-21
25Dog2019-05-27
26Bird2019-05-24

<tbody>
</tbody>
** In column B, I used data validation to create a list: 1, 2, 3




Sheet1 defines start and end date for each block. In column B of Sheet2, I'm hoping to generate either 1, 2, or 3 depending on the date entered in column D of Sheet2. So B1 would be 1, B2 would be 3, and B3 would be 2.

Any help will be much appreciated, thank you in advance :)
 
Last edited:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this in the B column and fill down. I made the assumption that (a) everything was on one sheet (adjust accordingly) and (b) the blocks are in the range A1 to C3.

Code:
=IF(AND(D6>=$A$2,D6<=$A$3),1,IF(AND(D6>=$B$2,D6<=$B$3),2,IF(AND(D6>=$C$2,D6<=$C$3),3,"??")))
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your Sheet2 and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter a date in column D and press the RETURN key.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Dim srcWS As Worksheet, x As Long
    Set srcWS = Sheets("Sheet1")
    For x = 1 To 3
        With srcWS
            If Target >= .Cells(2, x) And Target <= .Cells(3, x) Then
                Target.Offset(0, -2) = x
            End If
        End With
    Next x
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,870
Messages
6,122,019
Members
449,060
Latest member
LinusJE

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