OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 421
- Office Version
- 2019
- Platform
- 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.
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