I'm new on VBA, currently, I been seen the way to open a workbook base on creation date, I have found several codes but this open last save workbook or last modify file, sometimes last files are not the workbook I need and I will like to replace from last save file to the creation date.
Below its an example of code, I have found.
Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\Documents\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xls", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
Related
I update a spreadsheet three times a week that summarises business deliveries and other information.
The sheet requires me to dump in three or four intake reports each time to lookup the relevant data. I want to open the most recent file in a folder and copy the data into my active workbook.
I can't open the file. I get a run time error saying the file/path can't be found.
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
'specify the path to the folder
Mypath = "C:\Users\Documents"
'Make sure that the path ends in a backslash
If Right(Mypath, 1) <> "\" Then Mypath = Mypath & "\"
'Get the lfirst excel file from the folder
Myfile = Dir(Mypath & "*xlsx", 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 folder
Do While Len(Myfile) > 0
'If 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
The reason is simple: You never assign anything to LMD, so LMD is always 0 (that is the initial value for a date). As a consequence, LMD is never > LatestDate (which is also 0), you never assign any value to Myfile. At the end, you try to open a file with the name of your folder and that of course fails.
Simply add the FileDateTime command to fetch the file date:
LMD = FileDateTime(Mypath & Myfile)
If LMD > LatestDate Then
LatestFile = Myfile
LatestDate = LMD
End If
Hint: Learn to use the VBA debugger to check for such problems. I recommend to watch https://www.youtube.com/watch?v=Um2JwZfwoFI, but you can find a lot of other resources.
I used the below code to open the latest file in a folder and Vlookup from it and return the value which is in column I.
I am facing a Run-time error 1004, although everything is correct.
i Set wbname = ActiveWorkbook.Name to catch the open sheet name which I will put the Vlookup formula in and I am choosing the correct range for my formula which is I2, still can't figure out where did I go wrong.
Error Message in the below line:
Range("I2").Formula = _
"=VLOOKUP(A2,[" & MyPath & LatestFile & "]'Sheetname with input data'!A:I,9,False)"
My Code:
Sub PrepareforOutlookMails()
wbname = ActiveWorkbook.Name
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim wb As Workbook
Dim fileLocation As String
Dim fileToOpen As Workbook
MyPath = "C:\1.ER\1.Work\19.Etr\Recon\2022\October"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'first Excel file from the folder
MyFile = Dir(MyPath & "*.xls", vbNormal)
'If no files 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
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
Workbooks(wbname).Activate
Range("I2").Formula = _
"=VLOOKUP(A2,[" & MyPath & LatestFile & "]'Sheetname with input data'!A:I,9,False)"
Like I mentioned VLOOKUP works on closed file as well. There is no need to open the file.
Your [ and ] and ' placement is incorrect. Here is an example (Untested)
If you manualy type the formula, it will look like this
=VLOOKUP(D2,'C:\1.ER\1.Work\19.Etr\Recon\2022\October\[Mail Merge (Updated Sample File) (1).xlsx]Sheetname with input data'!A:I,9,0)
Sub Sample()
Dim MyPath As String
Dim LatestFile As String
MyPath = "C:\1.ER\1.Work\19.Etr\Recon\2022\October\"
LatestFile = "Mail Merge (Updated Sample File) (1).xlsx"
Range("I2").Formula = "=VLOOKUP(A2,'" & _
MyPath & _
"[" & _
LatestFile & _
"]Sheetname with input data'!A:I,9,0)"
End Sub
EDIT
This is how your original code can be written. I have commented the code so you should not have any problem understanding it.
Option Explicit
Sub PrepareforOutlookMails()
Dim wbThis As Workbook
Dim wsThis As Worksheet
Dim MyPath As String
Dim MyFile As String
Dim LMD As Date
Dim LatestFile As String
Dim LatestDate As Date
Set wbThis = ThisWorkbook
'~~> Change this to the relevant sheet
'~~> This is where the formula will be written
Set wsThis = wbThis.Sheets("Sheet1")
'MyPath = "C:\1.ER\1.Work\19.Etr\Recon\2022\October"
MyPath = "C:\Users\routs\Desktop"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
'~~> First Excel file from the folder
MyFile = Dir(MyPath & "*.xls*", vbNormal)
'~~> If no files 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
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
wsThis.Range("I2").Formula = "=VLOOKUP('" & wsThis.Name & "'!A2,'" & _
MyPath & _
"[" & _
LatestFile & _
"]Sheetname with input data'!A:I,9,0)"
End Sub
Screenshot
I am writing a macro to pull together data from different places each day. I would like to include data which is uploaded to a Sharepoint site. A new file is uploaded weekly, so I want the macro to find the most recent file in that folder.
I've tried the below code but a get runtime error 52 (Bad name). I have tried removing the https:, changing to backslash, as I saw this on google but still not working. Any help would be greatly appreciated!
'find & open latest XYZ File
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "https://company.sharepoint.com/sites/group/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2FMC%5FStockControllersandSupplyChain%2FShared%20Documents%2FGeneral%2FSupply%20Headlines&p=true&ga=1"
'MyPath = Replace(MyPath, "/", "\") 'adaptation attempt for SharePoint folder
'MyPath = Replace(MyPath, "https:", "") 'adaptation attempt for SharePoint folder
MyFile = Dir(MyPath & "*.xlsm")
If Len(MyFile) = 0 Then
MsgBox "No XYZ files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
I am wanting to write a VBA code to import the newest .csv file from a certain directory into a specific excel sheet. I am currently using a code that works, BUT it opens up in a new workbook and I cannot figure out how to append it to a certain sheet instead. If that makes sense? Also, is there a way to also delete all duplicates within that specific sheet after imported?
The code I am currently using through VBA is below....
Option Explicit
Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
MyPath = "C:\Users\Documents\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xls", vbNormal)
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
End Sub
The issue is that this code opens up in a new workbook instead of importing into a specific sheet.
I am using the following code (co-opted from someone here) to access the latest generated files from a folder. I use it to open the file and do various bits (example below). But I was wondering if there is a way to pull the filename from the latest file and paste it into a cell in another workbook?
Sub Macro3()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Application.DisplayAlerts = False
MyPath = "C:\Users\XXXX\Desktop\folderXXX"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "XYZ-********-123.csv", vbNormal)
If Len(MyFile) = 0 Then
'MsgBox "No files were found…", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile
Range("B2:D97").Copy
ActiveWindow.Close
Windows("New.xlsm").Activate
Range("A2").Select
ActiveSheet.Paste
End Sub
Can it been done with the existing code, is it as simple as adding something to this line Workbooks.Open MyPath & LatestFile instead of .Open?
Appreciate any help or tips even tips to tidy it up in any way. Thanks
You can do something like this:
Sub Macro3()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim wb As WorkBook, wsDest As Worksheet '<<<
Application.DisplayAlerts = False
MyPath = "C:\Users\XXXX\Desktop\folderXXX"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "XYZ-********-123.csv", vbNormal)
If Len(MyFile) = 0 Then
'MsgBox "No files were found…", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Set wsDest = ThisWorkbook.WorkSheets("Data") '<< for example
Set wb = Workbooks.Open(MyPath & LatestFile)
wb.Sheets(1).Range("B2:D97").Copy wsDest.Range("A2")
wb.Close False '<< don't save
wsDest.Range("A1").Value = LatestFile '<< record the file name
End Sub