Knockoutpie
Board Regular
- Joined
- Sep 10, 2018
- Messages
- 116
- Office Version
- 365
- Platform
- Windows
So I was able to get VBA to open the sheet name based on Month and Year (January 2020) sheet name for example, but if the month is currently December 1st, I don't want the December sheet, I want the November sheet because the data I'm using is November 30th. We will be working on the last line of code.. A Yesterday function would be great
Is there any way I can use the Now or Today function minus 1 day? Or can you help advise a way to do this?
Is there any way I can use the Now or Today function minus 1 day? Or can you help advise a way to do this?
VBA Code:
Dim folderPath As String, tableName As String, latestTblName As String
Dim latestModified As Date, modifiedDate As Date
folderPath = "Insert Path Name Here"
tableName = Dir(folderPath & "*.xlsx")
Do While tableName <> vbNullString
modifiedDate = FileDateTime(folderPath & tableName)
If latestModified < modifiedDate Then
latestModified = modifiedDate
latestTblName = tableName
End If
tableName = Dir()
Loop
Workbooks.Open folderPath & latestTblName
' can we open a file based on the month?
Sheets(UCase(Format(Today(), "mmmm YYYY"))).Select