open last modified worksheet

G

GEORGIA

I am new to VBA.
I wanted to have a macro open a last modified workbook in the folder.
I saw the one code in the discussion group, his did not work so i changed it
little bit.
here's my code:
Sub OpenMost()
Dim strFolderName As String


strFolderName = "D:\My Documents"

On Error GoTo Err_Handler

Dim strFileName
With Application.FileSearch
.LookIn = strFolderName
.FileType = msoFileTypeExcelWorkbooks
.SearchSubFolders = True
If .Execute(msoSortByLastModified, msoSortOrderDescending) > 0 Then
strFileName = .FoundFiles(1)
Application.Workbooks.Open strFileName
End If
End With
Exit_Sub:
Exit Sub
Err_Handler:
Resume Exit_Sub
End Sub

instead of opening the last modified file, it is sorting by the file name
and opening the first one.

what is wrong with this code?

Thank You
 
B

Bob Phillips

A known bug I believe. A separate priming call seems to sort it, it did for
me

Sub OpenMost()
Dim strFolderName As String


strFolderName = "D:\My Documents"

On Error GoTo Err_Handler

Dim strFileName
With Application.FileSearch
.NewSearch
.LookIn = strFolderName
.FileType = msoFileTypeExcelWorkbooks
.SearchSubFolders = True
.Execute SortBy:=msoSortBySize

.LookIn = strFolderName
.FileType = msoFileTypeExcelWorkbooks
.SearchSubFolders = True
If .Execute(msoSortByLastModified, msoSortOrderDescending) > 0 Then
strFileName = .FoundFiles(1)
Application.Workbooks.Open strFileName
End If
End With
Exit_Sub:
Exit Sub
Err_Handler:
Resume Exit_Sub
End Sub


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
B

Bob Phillips

I gave you the code to get around it.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

GEORGIA

duh! sorry!

THANK A BUNCH! IT WORKS PERFECTLY!

Bob Phillips said:
I gave you the code to get around it.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top