Opening Working book using vba

Nichole09

Board Regular
Joined
Aug 27, 2016
Messages
132
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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Even if it were to be able to open the file, it looks like it will never exit the Do While loop.
 
Upvote 0
I haven't studied your code, but at first glance, you have a conditional exit sub that will always ring true:

VBA Code:
filename = "G:\Administration\Review\" & myfile & Format(dtfile, "mmddyyyy") & ".xlsx"

If Len(filename) = 0 Then
    MsgBox "No Files were found.", vbExclamation
    Exit Sub
End If

Len(filename) = 0
is perpetually true.
 
Upvote 0
Your filename never changes, so replace the last bit of your code with ...
VBA Code:
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)
    On Error GoTo 0
    If wbk Is Nothing Then
        dtfile = dtfile - 1
        filename = "G:\Administration\Review\" & myfile & Format(dtfile, "mmddyyyy") & ".xlsx"
    End If
Loop
 
Upvote 0
Thank you everyone for the help!!! GWteB that did it!!! It pulls up the latest saved file. Thank you!! :)
 
Upvote 0
You are welcome & thanks for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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