msquared99
Member
Here is the situation. I have a report that I run from an external database. The report will always have a different name due to when it is run (several times a day), 03-21-13 AM1. When it is run it automatically opens in Excel 2007. I then open the last report in a folder for which I have VBA code that does that part and works well. This report is Any AR 03-20-13PM2 and changes often as well but the macro is looking for the last file run which takes care of this.
What I am doing is adding some formulas to workbook 03-21-13 AM1 then doing a vlookup from the workbook 03-20-13PM2 which the macro opened into the workbook from the database, 03-21-13 AM1.
I have to make this an easy process because not very skilled people will be using this was well.
My issue is how do I get the macro to identify the workbook run from the database?
Here is the macro that finds the last file saved in the folder:
[pre]
[/pre]
Thanks,
Mike
What I am doing is adding some formulas to workbook 03-21-13 AM1 then doing a vlookup from the workbook 03-20-13PM2 which the macro opened into the workbook from the database, 03-21-13 AM1.
I have to make this an easy process because not very skilled people will be using this was well.
My issue is how do I get the macro to identify the workbook run from the database?
Here is the macro that finds the last file saved in the folder:
[pre]
Code:
'Force the explicit delcaration of variables
Option Explicit
Sub OpenLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim path As Variant
path = InputBox(Prompt:="Enter the folder name. Must be MM.YYYY")
If path = 0 Then
MsgBox "No file name entered."
Exit Sub
End If
'Specify the path to the folder
MyPath = "B:AccountingDaily Reconciliation Reports3019-AR Report2013 3019 Reports" & path & ""
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
'Assign the date/time of the current file to a variable
LMD = FileDateTime(MyPath & MyFile)
'If the date/time of the current file is greater than the latest
'recorded date, assign its filename and date/time to variables
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
'Get the next Excel file from the folder
MyFile = Dir
Loop
'Open the latest file
Workbooks.Open MyPath & LatestFile
End Sub
Thanks,
Mike