ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   open last modified worksheet (https://www.excelbanter.com/excel-programming/333172-open-last-modified-worksheet.html)

GEORGIA

open last modified worksheet
 
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


Bob Phillips[_6_]

open last modified worksheet
 
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)


"GEORGIA" wrote in message
...
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




GEORGIA

open last modified worksheet
 
any ideas how to fix this "bug" or whatever it is ?
thank you

"Bob Phillips" wrote:

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)


"GEORGIA" wrote in message
...
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





Bob Phillips[_6_]

open last modified worksheet
 
I gave you the code to get around it.

--

HTH

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


"GEORGIA" wrote in message
...
any ideas how to fix this "bug" or whatever it is ?
thank you

"Bob Phillips" wrote:

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)


"GEORGIA" wrote in message
...
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







GEORGIA

open last modified worksheet
 
duh! sorry!

THANK A BUNCH! IT WORKS PERFECTLY!

"Bob Phillips" wrote:

I gave you the code to get around it.

--

HTH

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


"GEORGIA" wrote in message
...
any ideas how to fix this "bug" or whatever it is ?
thank you

"Bob Phillips" wrote:

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)


"GEORGIA" wrote in message
...
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









All times are GMT +1. The time now is 03:30 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com