VBA Code to Find a Date between two dates or find next nearest date after the end date

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Hello and thanks in advance. With VBA, In "RngSrch" I would then like to find any dates between and inclusive of the two given dates ("DateofPlan" and "DateEnd") and paste them into another sheet (let's say "Sheet1") starting with cell D9. If it does not find any dates between and including them, then return the next nearest date after "DateEnd"

So for example, if RngSrch has dates of 3/3/2022 and 3/4/2022 it will return those dates and stop. If RngSearch has dates of 2/28/2022 and 4/16/2022, it will return 4/16/2022.


VBA Code:
Sub DateFinder()

'Dim
    Dim DateofPlan As Variant
    Dim DateEnd As Variant
    Dim ShtNm As String
    Dim RngSrch As Range


'Set Search Values
    DateofPlan = "3/7/2022"
    DateEnd = DateAdd("d", 6, CDate(DateofPlan)) 'This will generate 3/16/2022

ShtNm = "Important.Dates"

'Set Range Search
    LR = LastRow(ShtNm)
    Set RngSrch = Range("A9:A" & LR)

'Code to Find the dates
    'I would then like to find any dates between and inclusive of the two given dates _
        and paste them into another sheet (let's say "Sheet1") starting with cell D9. _
        If it does not find any dates between and including them, then return the next _
        nearest date after "DateEnd"

End Sub


Function LastRowF(ByVal SheetName As String) As Long

    Dim WkS As Worksheet
        
        Set WkS = ActiveWorkbook.Worksheets(SheetName)
        
        With WkS
            LastRowF = .Cells.Find(What:="*", After:=WkS.Cells(1), _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        End With
        
End Function
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

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