Looking for Last Updated File and importing a specific sheet

Lloydmp

New Member
Joined
Jan 2, 2018
Messages
11
Hi there,

I have a bit of code that automatically looks for the last edited file and opens it.
It looks at the current folder and the previous folder in the file structure which is important.

What i would like to add is some code to open/copy/mirror a specific sheet into another workbook.

my code is as follow:

Code:
 Private Sub Workbook_Open()
Dim fdr, fdr2 As Scripting.Folder
  Dim target As Scripting.File
    For Each fdr In CreateObject("Scripting.FileSystemObject").GetFolder("M:\COMMUNAL\Run Outs\RUN OUTS 2018").SubFolders
        On Error Resume Next
        For Each target In fdr.Files
            If InStr(1, UCase(target), UCase(".xls")) > 0 Then
                If target.DateLastModified > dteFile Then
                    dteFile = target.DateLastModified
                    strFile = target
                End If
            End If
        Next
    Next
    DateFirstFile = dteFile
    FirstFile = strFile
    For Each fdr2 In CreateObject("Scripting.FileSystemObject").GetFolder("M:\COMMUNAL\Run Outs\RUN OUTS 2018").SubFolders
        On Error Resume Next
        For Each target In fdr2.Files
            If InStr(1, UCase(target), UCase(".xls")) > 0 Then
                If target.DateLastModified > dteFile Then
                    dteFile = target.DateLastModified
                    strFile = target
                End If
            End If
        Next
    Next
    DateSecondFile = dteFile
    SecondFile = strFile
    If DateFirstFile >= DateSecondFile Then
    If Len(Dir(FirstFile)) Then Workbooks.Open FirstFile
    ElseIf DateSecondFile >= DateFirstFile Then
    If Len(Dir(SecondFile)) Then Workbooks.Open SecondFile
    End If
End Sub

I'm a complete novice so any advice is helpful.
Thanks,

Lloyd
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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