Open workbook new method

LeonardSterk

New Member
Joined
Mar 21, 2021
Messages
33
Platform
  1. Windows
Hello,

I had a working macro that would open the most recent excel file. I made this macro at home for work. Now it turns out that this macro doesn't work at my work because we don't have permission to do this
The macro says we don't have permission, and no, I'm can't/not allowed to change permissions

VBA Code:
Sub Open()
    Workbooks.Open Split(CreateObject("WScript.Shell").Exec("cmd /c dir /b/o-d/s D:\Pro\Files\*.xls*").StdOut.ReadAll, vbCr)(0)
End Sub

Anyone know some alternative code to find the most recent file in D:\Pro\Files\ ?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try this
VBA Code:
Sub openLastModified()
Dim FolderPath As String, tableName As String, latestTblName As String
Dim latestModified As Date, modifiedDate As Date


FolderPath = "D:\Pro\Files\"


tableName = Dir(FolderPath & "*.xls")


Do While tableName <> vbNullString
    modifiedDate = FileDateTime(FolderPath & tableName)
    If latestModified < modifiedDate Then
        latestModified = modifiedDate
        latestTblName = tableName
    End If
    tableName = Dir()
Loop


Workbooks.Open FolderPath & latestTblName


End Sub
 
Upvote 0
Solution
Try this
VBA Code:
Sub openLastModified()
Dim FolderPath As String, tableName As String, latestTblName As String
Dim latestModified As Date, modifiedDate As Date


FolderPath = "D:\Pro\Files\"


tableName = Dir(FolderPath & "*.xls")


Do While tableName <> vbNullString
    modifiedDate = FileDateTime(FolderPath & tableName)
    If latestModified < modifiedDate Then
        latestModified = modifiedDate
        latestTblName = tableName
    End If
    tableName = Dir()
Loop


Workbooks.Open FolderPath & latestTblName


End Sub

YOU ROCK!!!
Unbelievable that worked first try.
Take that annoying company security !! >:)
 
Upvote 0
You're welcome
glad it works for you
I pasted some of my code below yours, it ends with closing the workbook but it asks if I want to save the file.
How can I make it in vbs to save the workbook?
I tried ActiveWorkbook.Save but that didn't work

VBA Code:
Sub openLastModified()
Dim FolderPath As String, tableName As String, latestTblName As String
Dim latestModified As Date, modifiedDate As Date


FolderPath = "D:\Pro\Files\"


tableName = Dir(FolderPath & "*.xls")


Do While tableName <> vbNullString
    modifiedDate = FileDateTime(FolderPath & tableName)
    If latestModified < modifiedDate Then
        latestModified = modifiedDate
        latestTblName = tableName
    End If
    tableName = Dir()
Loop


Workbooks.Open FolderPath & latestTblName

`mycode
ActiveWorkbook.Save
ActiveWorkbook.Close


End Sub
 
Upvote 0
So you want to open last modified file and then save and close same file?
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,849
Members
449,194
Latest member
HellScout

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