MS Excel VBA Code to Filter on Date Equal to or Next Future Date

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance. My goal with this VBA Code is to find the either date equal to
VBA Code:
DateRef
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.
Get Nearest Date Macro - (2022-04-14, V01) (version 2).xlsb
ABCDEFGHIJKLM
1
2
3
4
5
6
72022-06-17, Fri2023-02-17, Fri2022-07-04, MonIndependence Day2017-10-31, Tue2023-09-29, Fri2022-03-13, Sun
8REGION 1REGION 2HOLIDAY DATEHOLIDAYREGION 4REGION 3DAYLIGHT SAVINGS TIME
9 2021-03-19, Fri 2022-01-21, Fri 2021-11-25, ThuThanksgiving 2017-01-31, Tue 2022-03-31, Thu 2019-03-10, Sun
10 2021-06-18, Fri 2022-02-18, Fri 2021-12-24, FriChristmas 2017-03-14, Tue 2022-06-30, Thu 2019-11-03, Sun
11 2021-09-17, Fri 2022-03-18, Fri 2022-01-17, MonMartin Luther King, Jr. Day 2017-05-02, Tue 2022-09-30, Fri 2020-03-08, Sun
12 2022-02-21, MonWashington'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, FriGood 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, MonMemorial 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, MonIndependence 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, MonLabor 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, ThuThanksgiving 2018-01-30, Tue 2023-03-12, Sun
18 2023-03-17, Fri 2023-05-19, Fri 2022-12-30, FriNew Year's Day 2018-03-20, Tue 2023-11-05, Sun
19 2023-06-16, Fri 2023-06-16, Fri 2023-01-16, MonMartin Luther King, Jr. Day 2018-05-01, Tue 2024-03-10, Sun
20 2023-09-15, Fri 2023-07-21, Fri 2023-02-20, MonWashington's Birthday (Presidents' Day) 2018-06-12, Tue 2024-11-03, Sun
21 2023-12-15, Fri 2023-11-17, Fri 2023-05-29, MonMemorial 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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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