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

JennV

New Member
Joined
May 9, 2019
Messages
34
Sheet1:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]Block 1[/TD]
[TD="align: center"]Block 2[/TD]
[TD="align: center"]Block 3[/TD]
[/TR]
[TR]
[TD="align: center"]2019-05-20[/TD]
[TD="align: center"]2019-05-23[/TD]
[TD="align: center"]2019-05-27[/TD]
[/TR]
[TR]
[TD="align: center"]2019-05-22[/TD]
[TD="align: center"]2019-05-26[/TD]
[TD="align: center"]2019-05-31[/TD]
[/TR]
</tbody>[/TABLE]

Sheet2:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]24[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Cat[/TD]
[TD="align: center"]2019-05-21[/TD]
[/TR]
[TR]
[TD="align: center"]25[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Dog[/TD]
[TD="align: center"]2019-05-27[/TD]
[/TR]
[TR]
[TD="align: center"]26[/TD]
[TD="align: center"][/TD]
[TD="align: center"]Bird[/TD]
[TD="align: center"]2019-05-24[/TD]
[/TR]
</tbody>[/TABLE]
** 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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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