EileenJohn
Board Regular
- Joined
- Nov 23, 2016
- Messages
- 53
Hi, I'm a beginner in macros and I'm trying to:
1. Get the latest workbook from the DailyRecord folder.
2. Copy the latest workbook data to another workbook (Record.xlsx)
3. Set it on timer (daily auto update)
for now, i only manage to open latest workbook in DailyRecord folder. But i do wish to copy the data without opening the workbook
Here is my code (i get it from website) :
Sub CopyLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim src As Workbook
'Specify the path to the folder
MyPath = "C:\Users\user\Desktop\DailyRecord"
'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
Set src = Workbooks.Open(MyPath & LatestFile, True, True)
End Sub
[h=1][/h]
1. Get the latest workbook from the DailyRecord folder.
2. Copy the latest workbook data to another workbook (Record.xlsx)
3. Set it on timer (daily auto update)
for now, i only manage to open latest workbook in DailyRecord folder. But i do wish to copy the data without opening the workbook
Here is my code (i get it from website) :
Sub CopyLatestFile()
'Declare the variables
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim src As Workbook
'Specify the path to the folder
MyPath = "C:\Users\user\Desktop\DailyRecord"
'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
Set src = Workbooks.Open(MyPath & LatestFile, True, True)
End Sub
[h=1][/h]