VBA code to open most recent file saved in folder (Network UNC Folder)

redzer

New Member
Joined
Aug 29, 2017
Messages
3
Hi,

I have an Excel file where users select the most recent version of a file in a network folder and import it. I would like to automate this step so that the macro automatically imports the most recent file.

I have tried to incorporate the code from this answer but cannot figure out the syntax as I am using SetUNCPath.
https://www.mrexcel.com/forum/excel...b-open-most-recently-created-file-folder.html

Does anyone know how I could do this?

Code:
Sub CommandButton1_Click()

Dim wb As Workbook, wb2 As Workbook
Dim sPath As String


'Set source workbook
Set wb = ActiveWorkbook

sPath = "\\Network Path\Sub Folder"
 If SetUNCPath(sPath) <> 0 Then
    FileToOpen = Application.GetOpenFilename _
    ("CSV Files,*.csv", _
    1, "Select One File To Open", , False)
    ''
    
    If FileToOpen = False Then
         MsgBox "No file specified.", vbExclamation, "No File Specified"
         Exit Sub
        Else
         
         Workbooks.Open Filename:=FileToOpen
    
    End If

Else
 MsgBox "Error in setting the UNC path - " & sPath
 End If

Thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Apologies, I had left out the top of the code and I could not edit my post.

Code:
Private Declare Function SetCurrentDirectoryA Lib "kernel32" _
 (ByVal lpPathName As String) As Long
 
Function SetUNCPath(sPath As String) As Long
 Dim lReturn As Long
 lReturn = SetCurrentDirectoryA(sPath)
 SetUNCPath = lReturn
End Function

Sub CommandButton1_Click()

Dim wb As Workbook, wb2 As Workbook
Dim sPath As String

'Set source workbook
Set wb = ActiveWorkbook

sPath = "\\FilePath\Process Test"
 If SetUNCPath(sPath) <> 0 Then
    FileToOpen = Application.GetOpenFilename _
    ("CSV Files,*.csv", _
    1, "Select One File To Open", , False)
    ''
    
    If FileToOpen = False Then
         MsgBox "No file specified.", vbExclamation, "No File Specified"
         Exit Sub
        Else
         
         Workbooks.Open Filename:=FileToOpen
    
    End If

Else
 MsgBox "Error in setting the UNC path - " & sPath
 End If
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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