Import Sheet from Most Recent File in a Folder

David04Ruiz

New Member
Joined
Aug 29, 2022
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Hi all, I wanted to see if there was a way to import a sheet into my workbook from the most recent file in a specified folder? I know how to import a closed workbook based off of the file name but I wanted to import it based off the file creation date if possible. I want to import the first sheet of the closed most recent workbook. Super new to VBA so if anyone has a recommendation of how to do this id very much appreciate it!
 
The Dir is the second part of a pair of statements and you are doing all that in the function.
The function has already returned the File you want so your main routine does not need any of the looping.

Remove the struck out lines.

Rich (BB code):
Do While fileName <> ""
    Workbooks.Open (directory & fileName)

        Workbooks(fileName).Worksheets("Template").Copy _
            after:=Workbooks("Book1.xlsm").Worksheets("Sheet1")

    Workbooks(fileName).Close
    fileName = Dir()
Loop
This worked! My hopefully last questions is: can I add this code to the end of another macro I have so that I can just run it and it do all necessary functions at once? Or do I have to keep this as its own macro and just run it by itself after running the first one (Macro 1)? For reference, I included the code of the first macro below. Regardless, thank you both so much for your help, I really do appreciate it!

VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1").Select
    Range("AN1").Select
    ActiveCell.FormulaR1C1 = "Key Accounts"
    Cells.Select
    Range("AA1").Activate
    Selection.AutoFilter
    Columns("F:F").Select
    Selection.Style = "Currency"
    Columns("A:A").ColumnWidth = 60
    Columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Age"
    Range("A1").Select
    ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.ActiveSheet.AutoFilter.Sort.SortFields.Add2 _
        Key:=Range("A1:A5000"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.ActiveSheet.AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select

End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top