Hi all!
I have a macro that opens last modified file with partial name from specific folder. It returns mismatch error on
line and I have no clue why, can somebody explain it please?
I have a macro that opens last modified file with partial name from specific folder. It returns mismatch error on
VBA Code:
Set filToOpen = fil
VBA Code:
Private Sub OpenMaster_Click()
Const sPartialName As String = "Master File"
Dim sStartPath As String
Dim sFileExt As String
Dim fso, fldr, fil, S, myDate, filToOpen As Workbook
sStartPath = "C:\Local\Raports\"
sFileExt = "*.xlsx"
Set fso = CreateObject("Scripting.Filesystemobject")
Set fldr = fso.getfolder(sStartPath).Files
For Each fil In fldr
If fil.Name Like sPartialName & "*" & sFileExt Then
If fil.datelastmodified > myDate Then
myDate = fil.datelastmodified
Set filToOpen = fil 'This line returns mismatch
End If
End If
Next fil
If Not filToOpen Is Nothing Then
Workbooks.Open Filename:=filToOpen.Name
Else
MsgBox "No Master File found."
End If
End Sub