'I want to get the file on directory counting 21 previous days from today

I have a directory with subdirectories that contains monthly files archived. I want to get the data from files from subdirectory counting 21 previous days from today and if is not in my current subdirectory, then skip to previous subdirectory.

The way I declared, brings me the content that I need, however with exactly the currently month, number corresponding of month, and year.

Sub getNumber(CM As String, shift As String, fileDate As String)
Dim newDate: newDate = Format(DateAdd("M", 0, Now), "MM-MMMM")
filePath ="\\bt\depthr\cement\trailer\Plans\Day Shift\05-May\trailer_flow_5-5-22 Day Shift Plan.xlsb\" & newDate & "\"
fileName = CM & "_" & "flow" & "_" & fileDate & "Plans" & ".xlsb"```

How can I correct it to bring me previous 21 days and skip to previous subdirectory if the 21 days includes previous month?

Thank you.



Solution 1:[1]

This will give you 21 days back date in VBA.

Dim newD As Date
newD = Date - 21
MsgBox newD

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Naveed