OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for any responses. What is the simplest and most efficient way to write VBA Code to check if a series of dates (columns A and B within "Sheet1") are within the dates (Columns B and C) of each row in "Sheet2" and add a comment if it is. The following is the VBA Code I wrote which works, but I would like to see if there is a simpler method. It runs fairly fast, but the data set is small. I thought about storing the data sets in Sheet1 into Scripting Dictionaries, but not sure how to retrieve and compare.
"Sheet1"
"Sheet2" before running the code
"Sheet2" after running the code
VBA Code:
Option Explicit
'***************************************************************************************************************
Sub DateRangeCheck()
'_______________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
'_______________________________________________________________________________________________________
'Dimensioning
'Dim Longs
Dim i As Long, j As Long, NoSL As Long, NLR_Sht2 As Long
'Dim Strings
Dim Sht1 As String, Sht2 As String
Dim DataSet As String, CommentStr As String
'Dim Dates
Dim DateStart As Date, DateEnd As Date
'Dim Ranges
Dim aCell As Range, Rng As Range, Rng_Sht1_DS1 As Range, Rng_Sht1_DS2 As Range
'Dim Timer Variables
Dim BenchMark As Double
'_______________________________________________________________________________________________________
'Code - Timer Benchmark
BenchMark = Timer
'_______________________________________________________________________________________________________
'Code - set sheet names
Sht1 = "Sheet1"
Sht2 = "Sheet2"
'_______________________________________________________________________________________________________
'Code -
NLR_Sht2 = LastRowF(Sht2)
With Sheets(Sht2)
.Range("C" & NLR_Sht2).Clear
End With
'_______________________________________________________________________________________________________
'Code -
With Sheets(Sht1)
Set Rng_Sht1_DS1 = .Range(.Cells(9, 1), .Cells(24, 1))
Set Rng_Sht1_DS2 = .Range(.Cells(9, 2), .Cells(32, 2))
End With
'_______________________________________________________________________________________________________
'Code -
NoSL = 9 'starting row for the loop
NLR_Sht2 = LastRowF(Sht2) 'last row for the loop
With Sheets(Sht2)
For j = 1 To 2
If j = 1 Then
Set Rng = Rng_Sht1_DS1
DataSet = "DATA SET 1"
ElseIf j = 2 Then
Set Rng = Rng_Sht1_DS2
DataSet = "DATA SET 2"
End If
NoSL = 9
For Each aCell In Rng
For i = NoSL To NLR_Sht2
DateStart = .Cells(i, 1)
DateEnd = .Cells(i, 2)
CommentStr = .Cells(i, 3)
'Check if date is within start and end dates
If Not IsEmpty(DateStart) And aCell < DateStart Then
Exit For
ElseIf Not IsEmpty(DateStart) And aCell >= DateStart And aCell <= DateEnd Then
'Add the comment, but preserve prior comments
If CommentStr <> "" Then
CommentStr = CommentStr & " & " & DataSet & ": " & Format(aCell.Value, "YYYY-MM-DD, DDD")
.Cells(i, 3) = CommentStr
Else
CommentStr = DataSet & ": " & Format(aCell.Value, "YYYY-MM-DD, DDD")
.Cells(i, 3) = CommentStr
End If
NoSL = i
Exit For
End If
Next i
Next aCell
Next j
End With
'_______________________________________________________________________________________________________
'Place cursor in Workbook, Sheet, and Cell
'_______________________________________________________________________________________________________
'Turn on alerts and screen updates, and calculate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Calculate
'_______________________________________________________________________________________________________
'Timer
MsgBox Round(Timer - BenchMark, 2)
'_______________________________________________________________________________________________________
'End of the subroutine/macro
End Sub
"Sheet1"
Test Date Pull.xlsx | ||||
---|---|---|---|---|
A | B | |||
8 | DATE SET 1 | DATE SET 2 | ||
9 | 2021-01-26, Tue | 2021-01-13, Wed | ||
10 | 2021-03-16, Tue | 2021-02-10, Wed | ||
11 | 2021-04-27, Tue | 2021-03-16, Tue | ||
12 | 2021-06-15, Tue | 2021-04-13, Tue | ||
13 | 2021-07-27, Tue | 2021-05-12, Wed | ||
14 | 2021-09-21, Tue | 2021-06-10, Thu | ||
15 | 2021-11-02, Tue | 2021-07-13, Tue | ||
16 | 2021-12-14, Tue | 2021-08-11, Wed | ||
17 | 2022-01-25, Tue | 2021-09-14, Tue | ||
18 | 2022-03-15, Tue | 2021-10-13, Wed | ||
19 | 2022-05-03, Tue | 2021-11-10, Wed | ||
20 | 2022-06-14, Tue | 2021-12-10, Fri | ||
21 | 2022-07-26, Tue | 2022-01-12, Wed | ||
22 | 2022-09-20, Tue | 2022-02-10, Thu | ||
23 | 2022-11-01, Tue | 2022-03-10, Thu | ||
24 | 2022-12-13, Tue | 2022-04-12, Tue | ||
25 | 2022-05-11, Wed | |||
26 | 2022-06-10, Fri | |||
27 | 2022-07-13, Wed | |||
28 | 2022-08-10, Wed | |||
29 | 2022-09-13, Tue | |||
30 | 2022-10-13, Thu | |||
31 | 2022-11-10, Thu | |||
32 | 2022-12-13, Tue | |||
Sheet1 |
"Sheet2" before running the code
Test Date Pull.xlsx | |||||
---|---|---|---|---|---|
A | B | C | |||
8 | DATE | DATE | COMMENT | ||
9 | 2021-03-08, Mon | 2021-03-12, Fri | |||
10 | 2021-03-15, Mon | 2021-03-19, Fri | |||
11 | 2021-03-22, Mon | 2021-03-26, Fri | |||
12 | |||||
13 | 2021-06-07, Mon | 2021-06-11, Fri | |||
14 | 2021-06-14, Mon | 2021-06-18, Fri | |||
15 | 2021-06-21, Mon | 2021-06-25, Fri | |||
16 | |||||
17 | 2021-09-07, Tue | 2021-09-10, Fri | |||
18 | 2021-09-13, Mon | 2021-09-17, Fri | |||
19 | 2021-09-20, Mon | 2021-09-24, Fri | |||
20 | |||||
21 | 2021-12-06, Mon | 2021-12-10, Fri | |||
22 | 2021-12-13, Mon | 2021-12-17, Fri | |||
23 | 2021-12-20, Mon | 2021-12-23, Thu | |||
24 | |||||
25 | 2022-03-07, Mon | 2022-03-11, Fri | |||
26 | 2022-03-14, Mon | 2022-03-18, Fri | |||
27 | 2022-03-21, Mon | 2022-03-25, Fri | |||
28 | |||||
29 | 2022-06-06, Mon | 2022-06-10, Fri | |||
30 | 2022-06-13, Mon | 2022-06-17, Fri | |||
31 | 2022-06-21, Tue | 2022-06-24, Fri | |||
32 | |||||
33 | 2022-09-06, Tue | 2022-09-09, Fri | |||
34 | 2022-09-12, Mon | 2022-09-16, Fri | |||
35 | 2022-09-19, Mon | 2022-09-23, Fri | |||
Sheet2 |
"Sheet2" after running the code
Test Date Pull.xlsx | |||||
---|---|---|---|---|---|
A | B | C | |||
8 | DATE | DATE | COMMENT | ||
9 | 2021-03-08, Mon | 2021-03-12, Fri | |||
10 | 2021-03-15, Mon | 2021-03-19, Fri | DATA SET 1: 2021-03-16, Tue & DATA SET 2: 2021-03-16, Tue | ||
11 | 2021-03-22, Mon | 2021-03-26, Fri | |||
12 | |||||
13 | 2021-06-07, Mon | 2021-06-11, Fri | DATA SET 2: 2021-06-10, Thu | ||
14 | 2021-06-14, Mon | 2021-06-18, Fri | DATA SET 1: 2021-06-15, Tue | ||
15 | 2021-06-21, Mon | 2021-06-25, Fri | |||
16 | |||||
17 | 2021-09-07, Tue | 2021-09-10, Fri | |||
18 | 2021-09-13, Mon | 2021-09-17, Fri | DATA SET 2: 2021-09-14, Tue | ||
19 | 2021-09-20, Mon | 2021-09-24, Fri | DATA SET 1: 2021-09-21, Tue | ||
20 | |||||
21 | 2021-12-06, Mon | 2021-12-10, Fri | DATA SET 2: 2021-12-10, Fri | ||
22 | 2021-12-13, Mon | 2021-12-17, Fri | DATA SET 1: 2021-12-14, Tue | ||
23 | 2021-12-20, Mon | 2021-12-23, Thu | |||
24 | |||||
25 | 2022-03-07, Mon | 2022-03-11, Fri | DATA SET 2: 2022-03-10, Thu | ||
26 | 2022-03-14, Mon | 2022-03-18, Fri | DATA SET 1: 2022-03-15, Tue | ||
27 | 2022-03-21, Mon | 2022-03-25, Fri | |||
28 | |||||
29 | 2022-06-06, Mon | 2022-06-10, Fri | DATA SET 2: 2022-06-10, Fri | ||
30 | 2022-06-13, Mon | 2022-06-17, Fri | DATA SET 1: 2022-06-14, Tue | ||
31 | 2022-06-21, Tue | 2022-06-24, Fri | |||
32 | |||||
33 | 2022-09-06, Tue | 2022-09-09, Fri | |||
34 | 2022-09-12, Mon | 2022-09-16, Fri | DATA SET 2: 2022-09-13, Tue | ||
35 | 2022-09-19, Mon | 2022-09-23, Fri | DATA SET 1: 2022-09-20, Tue | ||
Sheet2 |