Hello!! I have the below code to attempt to open the latest workbook with a certain name. For some reason, if the current date is not found the below code should keep subtracting 1 day from today's date and keep looking. But when I run the code, it only looks for today's date over and over again. I cant understand why it is not recognizing the updated date. Please help! I would greatly appreciate it!
VBA Code:
Sub compare()
Dim last As Long
Dim filename As String, myfile As String
Dim strfile As String, dtfile As Date
Dim current As Integer, getweeknumber As Integer
Set ws1 = Workbooks("Review").Sheets("Ops")
current = DatePart("q", ws1.range("d8").Value, 2)
getweeknumber = Int((13 + Day(ws1.range("d8").Value) - Weekday((ws1.range("d8").Value), vbMonday) - 5) / 7)
If current = 1 And getweeknumber = 2 Then
myfile = "MT.WesternMontana_"
ElseIf current = 1 And getweeknumber > 3 Then
myfile = "WY.GreaterWyoming_"
ElseIf current = 2 And getweeknumber = 2 Then
myfile = "WY.CheyenneWyoming_"
ElseIf current = 2 And getweeknumber > 3 Then
myfile = "SD.SouthDakota-MT.Montana_"
ElseIf current = 3 And getweeknumber = 2 Then
myfile = "OR.Oregon-WA.Washington_"
ElseIf current = 3 And getweeknumber > 3 Then
myfile = "ID.Idaho-WA.Washington_"
Else
End If
dtfile = Date
'dtfile = dateadd("m" -1, now())
' use the above comment if need to look back a month
filename = "G:\Administration\Review\" & myfile & Format(dtfile, "mmddyyyy") & ".xlsx"
If Len(filename) = 0 Then
MsgBox "No Files were found.", vbExclamation
Exit Sub
End If
Do While Len(filename) > 0
On Error Resume Next
Set wbk = Workbooks.Open(filename)
If wbk Is Nothing Then
dtfile = dtfile - 1
Else
End If
On Error GoTo 0
Loop
End Sub