Need help with opening files with VBA

cob98tp

Board Regular
Joined
Nov 18, 2004
Messages
146
Morning All,

I need a litle help with a macro I use to open a series of files...

We have a system with produces files each day called...

"example, dd-mm-yyyy hh-mm-ss.xls"

Where dd-mm-yyyy is the date the report was created, and hh-mm-ss is the exact time the report was finished.

The problem is that the time is never the same and not predictable. I want a piece of code that will open the file with the latest date and time in the directory when the macro is run and rename it to just "example.xls", before performing some other code which I already have and definitely works.

The date is usually going to be the previous day - except after a weekend/bank holiday. And it doesn't matter is the file is deleted after use .. so it could be possible to open the only file of the format "example, dd-mm-yyyy hh-mm-ss.xls" I just don't know how...

I'm sure I'm close, I'm just missing a little bit of code to make it all come together.

Thanks in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Cob,

When you run OpenLatestFile it will do exactly that - you can then do a ActiveWorkbook.SaveAs to rename it.

Change path & filename accordingly...

Code:
Function LatestFile(ByVal pPathExpression As String) As String
Dim strFile As String
    
strFile = Dir("C:\My Path\example *.xls") 'path & file name - customize as required
LatestFile = strFile
    
Do While strFile <> ""
    If strFile > LatestFile Then LatestFile = strFile
    strFile = Dir
Loop

End Function

Sub OpenLatestFile()
Dim strPath As String
Dim strFileExpr As String
Dim strLatestFile As String

strPath = "C:\My Path\" 'path to search - customize as required
strFileExpr = "example *.xls" 'file names to find - again change to suit

strLatestFile = strPath & LatestFile(strPath & strFileExpr)

Workbooks.Open (strLatestFile)

End Sub

Hope this helps
PT
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,777
Members
449,049
Latest member
greyangel23

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