Pookiemeister
Well-known Member
- Joined
- Jan 6, 2012
- Messages
- 626
- Office Version
- 365
- 2010
- Platform
- Windows
There are four of us that are tasked with code creating multiple spreadsheets that tracks a lot of data. This is one big project that we decided to split in half. Two of us will take one half. One spreadsheet we will be creating will be named: "NotTheRealName1" and the other will be named "NotTheRealName2". We've decided to use github for this project.
I found this code online and pasted in the workbook all these spreadsheets would be going into. In order for this code to work, I had to enable the "Microsoft Scripting Runtime" within references under "Tools". I did modify it a little to make it more dynamic. I'm not sure exactly what all this does but I am sure, I don't need a lot of it.
1. To start, I just need it to find the line to modify that will find these two files (one at a time).
2. Once it finds one of the files, it will open it, Copy the entire spreadsheet (ctrl+A) and then finds the spreadsheet within the workbook with the exact name and then it will paste the it starting in Range("A1").
I found this code online and pasted in the workbook all these spreadsheets would be going into. In order for this code to work, I had to enable the "Microsoft Scripting Runtime" within references under "Tools". I did modify it a little to make it more dynamic. I'm not sure exactly what all this does but I am sure, I don't need a lot of it.
1. To start, I just need it to find the line to modify that will find these two files (one at a time).
2. Once it finds one of the files, it will open it, Copy the entire spreadsheet (ctrl+A) and then finds the spreadsheet within the workbook with the exact name and then it will paste the it starting in Range("A1").
VBA Code:
Sub ImportMostRecentFile()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim MyFolder As Scripting.Folder
Set MyFolder = fso.GetFolder("C:\Users\" & Environ("username") & "\Downloads")
Dim Fle As Scripting.File
Dim NewestFile As Scripting.File
ThisWorkbook.FollowHyperlink Address:="https://github.com/login", NewWindow:=True
For Each Fle In MyFolder.Files
If NewestFile Is Nothing Then Set NewestFile = Fle
If NewestFile.DateCreated < Fle.DateCreated Then Set NewestFile = Fle
Next Fle
Workbooks.Open NewestFile
ActiveWorkbook.Worksheets("Sheet1").Copy after:=Workbooks("Import Latest File.xlsm").Worksheets("Sheet1")
ActiveSheet.Name = NewestFile.Name
Workbooks(NewestFile.Name).Close
End Sub