+ Reply to Thread
Results 1 to 2 of 2

need to open latest excel workbook automatically

Hybrid View

  1. #1
    Registered User
    Join Date
    05-04-2015
    Location
    Irvine, CA
    MS-Off Ver
    2010
    Posts
    37

    need to open latest excel workbook automatically

    I have been using the following code successfully for a while now. Upon activation of macro, the program prompts the user to open an excel file , with the specific folder path already open. From there, the user double clicks (opens) the selected file and the programming continues. However, I would like to modify it slightly so the user DOES NOT have to select his/her own file, but Excel will just automatically find the newest file with "Date Last Modified" (usually the first file on top in my share drive) upon activation. Can anyone assist me? It seems relatively simple but couldn't get any short and concise responses online.

    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim myFile As String
    Dim YourFolderPath As Variant
    Dim y As Integer, yY As Integer, x As Integer, xX As Integer
    Dim pT As PivotTable, pI As PivotItem
    Dim ws As Worksheet
    
    
    Application.ScreenUpdating = False
    'Turn Off Automatic Calculation During Macro Execution (At least one Worksheet Must be Shown)
    Application.Calculation = xlCalculationManual
    
    
    ' this workbook
    Set wb1 = ActiveWorkbook
    
        
    'initiate Open window with folder destinatoin
    YourFolderPath = "S:\Department\Private"
    ChDrive YourFolderPath
    ChDir YourFolderPath
    myFile = Application.GetOpenFilename(, , Title:="S:\Department\Private")
    
    If myFile = "False" Then Exit Sub
    
    'MsgBox myFile
    Set wb2 = Workbooks.Open(fileName:=myFile)

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: need to open latest excel workbook automatically

    Hi there,

    Try the following code and see if it does what you need:

    
    
    Sub OpenLatestFile()
    
        Const sFILE_PATH    As String = "S:\Department\Private\"
    
        Dim dteLatestFile   As Date
        Dim bFileExists     As Boolean
        Dim dteThisFile     As Date
        Dim sFileName       As String
        Dim vFile           As Variant
    
        sFileName = vbNullString
        dteLatestFile = 0
    
        On Error Resume Next
            vFile = Dir(sFILE_PATH, vbNormal)
        On Error GoTo 0
    
        If Not IsEmpty(vFile) Then
              bFileExists = True
        Else: MsgBox "The folder """ & sFILE_PATH & """ cannot be located", vbExclamation
              bFileExists = False
        End If
    
        While bFileExists = True
    
            If vFile = vbNullString Then
    
                  bFileExists = False
    
            Else: dteThisFile = FileDateTime(sFILE_PATH & vFile)
    
                  If dteThisFile > dteLatestFile Then
                      dteLatestFile = dteThisFile
                      sFileName = vFile
                  End If
    
                  vFile = Dir
    
            End If
    
        Wend
    
        If sFileName <> vbNullString Then
            Workbooks.Open Filename:=sFILE_PATH & sFileName
        End If
    
    End Sub
    The highlighted value can be changed to suit your requirements.

    The above routine will open the file most recently modified in the specified folder. It assumes that the most recently modified files in the folder will always be Excel workbooks.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 09-11-2015 at 04:32 PM. Reason: Minor addition

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2014, 06:04 AM
  2. Replies: 2
    Last Post: 03-09-2014, 01:38 PM
  3. Replies: 3
    Last Post: 03-09-2014, 01:37 PM
  4. [SOLVED] Automatically open the latest date modified excel file in a folder
    By arleutwyler in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2014, 01:35 PM
  5. Replies: 9
    Last Post: 11-28-2013, 05:20 PM
  6. How to open the Excel sheet with latest date
    By vijayvarshan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-27-2013, 06:30 AM
  7. How To Automatically Open New Workbook After 1st Workbook Open 4 Seconds
    By Launchnet in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-09-2009, 12:55 AM
  8. Open Latest TextFile with latest TimeStamp
    By pr4t3ek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2009, 10:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1