[VBA] Throwing in the towel. Need to open the latest file in a folder based on the file name, and if not there, open the most recent before that.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
788
Office Version
  1. 2010
Platform
  1. Windows
Hello. This is driving me nuts and they've removed the partitions in the office so I can't concentrate.

I have a set of files in a folder called "2020 Regional Press Report - Week Commencing [date].xlsx", where the date is in format 2020-02-24 etc (yyyy-mm-dd)

What I need to do is either:

Take the current week commencing date as that is when the report should be ran. In this case it is 2020-02-24. Save that as a string, then push it in to open the report, set as "rpr"

OR

Loop through the folder until it finds the most recent file by filename (not modified, as files can be altered after the fact), then open it.


This process is being completed within a large time-sensitive loop, so speed is definitely a requirement, which is why I am leaning towards the first option, although we may work a week in advance and thus don't have anything available for that week. An example is if we were working a week in advance, this would try to open 2020-03-02, but of course this doesn't exist yet, so in that instance I need it to open "filename - 7", to then go back to 2020-02-24 where it will open the file.

Here is what I have:

VBA Code:
Dim Dirfile As String
Dim rpr As Workbook
idp = Format(Date + 7 - Weekday(Date, vbUseSystemDayOfWeek) + 1, "yyyy-mm-dd")

Dirfile = "\\CompanyServer\Weeks\2020\2020 Regional Press Report - Week Commencing " & idp & ".xlsx"

rgt = Right(Split(Dirfile, ".")(0), 10)

fgd = idp - 7 'not working

If Right(Dirfile, 10) <> idp Then
Dirfile = "\\CompanyServer\Weeks\2020\2020 Regional Press Report - Week Commencing " & idp - 7 & ".xlsx"
Set rpr = Workbooks.Open(Dirfile)
Else
Set rpr = Workbooks.Open(Dirfile)
End If

Tearing my hair out here trying to work it out. I feel like I'm doing something criminally wrong because I have a date in "idp" but I can't subtract 7 from it.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I feel like I'm doing something criminally wrong because I have a date in "idp" but I can't subtract 7 from it.
Format turns it into text rather than a date so maybe
VBA Code:
fgd = DateValue(idp) - 7
Newfgd = Format(fgd, "yyyy-mm-dd")
Then use Newfgd
 
Upvote 0
Format turns it into text rather than a date so maybe
VBA Code:
fgd = DateValue(idp) - 7
Newfgd = Format(fgd, "yyyy-mm-dd")
Then use Newfgd

Hi, this makes a lot of sense. I've changed it around like this:

VBA Code:
idp = CLng(Date - Weekday(Date, vbUseSystemDayOfWeek) + 1)

DStr = Format(idp, "yyyy-mm-dd")

StrRPR = "\\CompanyServer\Weeks\2020\2020 Regional Press Report - Week Commencing " & DStr & ".xlsx"
StrTrue = Dir(StrRPR)


If StrTrue = "" Then
StrRPR = "\\CompanyServer\Weeks\2020\2020 Regional Press Report - Week Commencing " & Format(idp - 7, "yyyy-mm-dd") & ".xlsx"
Set rpr = Workbooks.Open(StrRPR)
Else
Set rpr = Workbooks.Open(StrRPR)
End If

This is a better way to check if the file is blank for me using Dir. If it can't find the file, then I check for the same date but minus 7 days.
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,416
Members
448,960
Latest member
AKSMITH

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