Find the latest version

Nlhicks

Active Member
Joined
Jan 8, 2021
Messages
264
Office Version
  1. 365
Platform
  1. Windows
Set Wb = Workbooks.Open("WAPA-UGPR Facility Rating and SOL Record (Data File)_v159.xlsx")
Wb.Sheets("Facility Ratings & SOLs (Lines)").Activate
Set Sheet2 = Sheets("Facility Ratings & SOLs (Lines)")
Set Ws = Sheet2.UsedRange

This code will take the Wb variable and set it equal to the latest version of the workbook but I am telling it to set it equal to the exact one. How can I make this more generic so that it will set it equal to the last version saved? When I close the workbook it automatically up revs to the next version so it would become 160. This code will not work on version 160 it will only work on 159. How can I word it so that it will work on whatever version I am wanting to make changes to?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi Nlhicks. The following code will give you the complete file path of the most recently saved .xlsx file in some specified folder. Not sure if that's really what you're after? Dave
Code:
Function NewestFile(FlderName As String) As String
Dim objfso As Object, objfolder As Object, objfile As Object
Dim NewFileName As String, NewFileDate As String, Temp As String
'returns newest .xlsm file in inputted folder
'ie. Call NewestFile("c:\users\dlsfa\documents\datafiles\")
Set objfso = CreateObject("scripting.filesystemobject")
Set objfolder = objfso.GetFolder(FlderName)
NewFileDate = vbNullString
For Each objfile In objfolder.Files
If InStr(objfile.Name, ".xlsx") Then
Temp = objfile.DateCreated
If Temp > NewFileDate Then
NewFileDate = Temp
NewFileName = objfile.Path 'Name
End If
End If
Next objfile
'MsgBox "Newest File: " & NewFileName & " created " & NewFileDate
NewestFile = NewFileName
Set objfolder = Nothing
Set objfso = Nothing
End Function
To operate eg....
Code:
Call NewestFile("c:\users\dlsfa\documents\datafiles\")

[
 
Upvote 0
Is there any way I can make my macro look at the latest version of a workbook and do what I need it to do to that workbook? Everytime I save the workbook it will create a new version number which is fantastic however, my macro will forever look at version v158 unless I can write it to look at the latest version. I am not sure how to do that, any ideas?
 
Upvote 0
I guess I should have added how you could use it. You need to adjust the folder path to suit. HTH. Dave
Code:
Dim wb As Workbook
Set wb = Workbooks.Open(NewestFile("c:\users\dlsfa\documents\datafiles\"))
'or... Workbooks.Open Filename:=NewestFile("c:\users\dlsfa\documents\datafiles\")
'or.. Dim NewFile as string
'.....NewFile = NewestFile("c:\users\dlsfa\documents\datafiles\")
'.....MsgBox NewFile
 
Upvote 0
Solution

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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