I'm trying to combine these two macros:
Macro 1: This finds a file and copies the sheet to another workbook. I need to find the latest file with the prefix "Car Assignments", this only works if I manually type the full file name.
Sub cpy()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("\\Desktop\ARC Dev\New folder\Car Assignments 11-16-2020.xlsx")
Set y = Workbooks.Open("\\Desktop\ARC Dev\arcgoat.xlsm")
'Now, copy what you want from x:
x.Sheets("Portfolio Assignments").Range("A1:U920").Copy
'Now, paste to y worksheet:
y.Sheets("Portfolio Assignments").Range("A1").PasteSpecial
'Close x:
x.Close
MsgBox ("Completed")
End Sub
Macro 2: This finds the most recent file in the directory, however, it does not account for files with the prefix 'Car Assignments'.
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 = "\\Desktop\ARC Dev\New folder\"
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyFile = Dir(MyPath & "*.xlsx", 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
I am trying to copy the latest Excel file in "\Desktop\ARC Dev\New folder" directory that has the name "Car Assignments" in it, and then copy that sheet to a workbook called arcgoat in the "\Desktop\ARC Dev\arcgoat.xlsm" directory.