Hi,
Please can anyone assist with a Macro. I need to be able to copy data on Sheet1 from the newest file in a file location and paste in a designated sheet on my Active File.
I can currently run a macro to open the latest file, but I want to just automate the copy and past exercise so that I can fully automate the whole report.
Example.
1. New Report
2. Copy Sheet1 from File X - which is located in C Drive location (newest file in the folder must be opened)
3. Paste Sheet1 to Sheet 4 on active open 'New Report'
The Macro I am currently using to open the newest file is written below, If this can be adapted to achieve the above I dont know.
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\Chris\Documents\Excel VBA Test"
'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
Many Thanks for any Help.
Please can anyone assist with a Macro. I need to be able to copy data on Sheet1 from the newest file in a file location and paste in a designated sheet on my Active File.
I can currently run a macro to open the latest file, but I want to just automate the copy and past exercise so that I can fully automate the whole report.
Example.
1. New Report
2. Copy Sheet1 from File X - which is located in C Drive location (newest file in the folder must be opened)
3. Paste Sheet1 to Sheet 4 on active open 'New Report'
The Macro I am currently using to open the newest file is written below, If this can be adapted to achieve the above I dont know.
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\Chris\Documents\Excel VBA Test"
'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
Many Thanks for any Help.