OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for your assistance. My goal with this VBA Code is to find the either date equal to
or the next nearest future date. I would like it to ignore any columns with that are blank or do not have any dates.
I have only included the code that I am having issues with and have tested it. It does ignore the blank columns, but the issues are:
Three questions:
(1) Why does it keep the filter on column 1?
(2) Why doesn't it get the matching date or the next nearest future date (e.g. Range("D7") = 2022-07-04, Mon vs Range("D7") = 2022-04-15, Fri).
(2) It retrieves a value for column F which has no dates (e.g. Range("E7") = Independence Day). For now, I want it to return a blank value.
Sample Data with the retrieved values in row 7 and highlighted in yellow.
Code is as follows:
VBA Code:
DateRef
I have only included the code that I am having issues with and have tested it. It does ignore the blank columns, but the issues are:
Three questions:
(1) Why does it keep the filter on column 1?
(2) Why doesn't it get the matching date or the next nearest future date (e.g. Range("D7") = 2022-07-04, Mon vs Range("D7") = 2022-04-15, Fri).
(2) It retrieves a value for column F which has no dates (e.g. Range("E7") = Independence Day). For now, I want it to return a blank value.
Sample Data with the retrieved values in row 7 and highlighted in yellow.
Get Nearest Date Macro - (2022-04-14, V01) (version 2).xlsb | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | |||
1 | |||||||||||||||
2 | |||||||||||||||
3 | |||||||||||||||
4 | |||||||||||||||
5 | |||||||||||||||
6 | |||||||||||||||
7 | 2022-06-17, Fri | 2023-02-17, Fri | 2022-07-04, Mon | Independence Day | 2017-10-31, Tue | 2023-09-29, Fri | 2022-03-13, Sun | ||||||||
8 | REGION 1 | REGION 2 | HOLIDAY DATE | HOLIDAY | REGION 4 | REGION 3 | DAYLIGHT SAVINGS TIME | ||||||||
9 | 2021-03-19, Fri | 2022-01-21, Fri | 2021-11-25, Thu | Thanksgiving | 2017-01-31, Tue | 2022-03-31, Thu | 2019-03-10, Sun | ||||||||
10 | 2021-06-18, Fri | 2022-02-18, Fri | 2021-12-24, Fri | Christmas | 2017-03-14, Tue | 2022-06-30, Thu | 2019-11-03, Sun | ||||||||
11 | 2021-09-17, Fri | 2022-03-18, Fri | 2022-01-17, Mon | Martin Luther King, Jr. Day | 2017-05-02, Tue | 2022-09-30, Fri | 2020-03-08, Sun | ||||||||
12 | 2022-02-21, Mon | Washington's Birthday (Presidents' Day) | 2017-06-13, Tue | 2022-12-30, Fri | 2020-11-01, Sun | ||||||||||
13 | 2021-12-17, Fri | 2022-04-14, Thu | 2022-04-15, Fri | Good Friday | 2017-07-25, Tue | 2023-03-31, Fri | 2021-03-14, Sun | ||||||||
14 | 2022-03-18, Fri | 2023-01-20, Fri | 2022-05-30, Mon | Memorial Day | 2017-09-19, Tue | 2023-06-30, Fri | 2021-11-07, Sun | ||||||||
15 | 2022-06-17, Fri | 2023-02-17, Fri | 2022-07-04, Mon | Independence Day | 2017-10-31, Tue | 2023-09-29, Fri | 2022-03-13, Sun | ||||||||
16 | 2022-09-16, Fri | 2023-03-17, Fri | 2022-09-05, Mon | Labor Day | 2017-12-12, Tue | 2023-12-29, Fri | 2022-11-06, Sun | ||||||||
17 | 2022-12-16, Fri | 2023-04-21, Fri | 2022-11-24, Thu | Thanksgiving | 2018-01-30, Tue | 2023-03-12, Sun | |||||||||
18 | 2023-03-17, Fri | 2023-05-19, Fri | 2022-12-30, Fri | New Year's Day | 2018-03-20, Tue | 2023-11-05, Sun | |||||||||
19 | 2023-06-16, Fri | 2023-06-16, Fri | 2023-01-16, Mon | Martin Luther King, Jr. Day | 2018-05-01, Tue | 2024-03-10, Sun | |||||||||
20 | 2023-09-15, Fri | 2023-07-21, Fri | 2023-02-20, Mon | Washington's Birthday (Presidents' Day) | 2018-06-12, Tue | 2024-11-03, Sun | |||||||||
21 | 2023-12-15, Fri | 2023-11-17, Fri | 2023-05-29, Mon | Memorial Day | 2025-03-09, Sun | ||||||||||
22 | 2025-11-02, Sun | ||||||||||||||
23 | |||||||||||||||
Important.Dates |
Code is as follows:
VBA Code:
Option Explicit
'***************************************************************************************************************
Sub FilterDates()
'_______________________________________________________________________________________________________________
'Turn off alerts, screen updates, and automatic calculation
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlManual
'_______________________________________________________________________________________________________________
'Dimensioning
'Dim longs
Dim j As Long
Dim RowNoStart As Long
Dim RowNoEnd As Long
Dim ColNo As Long
'Dim strings
Dim DateName As String
'Dim Dates
Dim DateRef As Date
Dim DateEvent As Date
'Dim Timer variables
Dim BenchMark As Double
'Dim Ranges
Dim RngSrchDate As Range
'______________________________________________________________________________________________________________
'Code -
DateName = "Plan.2022.04.18"
DateRef = Left(Right(TabName, 5), 2) & "/" & Right(TabName, 2) & "/" & Left(Right(TabName, 10), 4)
RowNoStart = 8
For j = 1 To 12
Cells(RowNoStart - 1, j).Interior.ColorIndex = 0
RowNoEnd = Cells(Rows.Count, j).End(xlUp).Row
Set RngSrchDate = Range(Cells(RowNoStart, j), Cells(RowNoEnd, j))
RngSrchDate.AutoFilter Field:=1, Criteria1:=">=" & DateRef
Cells(RowNoStart - 1, j) = _
RngSrchDate.Offset(1, 0).SpecialCells(xlCellTypeVisible)(1).Value
Cells(RowNoStart - 1, j).NumberFormat = "YYYY-MM-DD, DDD"
If Cells(RowNoStart - 1, j) <> "" Then
Cells(RowNoStart - 1, j).Interior.ColorIndex = 6
End If
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
Next j
'_________________________________________________________________________________________________________________
'Turn on alerts and screen updates, and calculate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Calculate
End Sub