how do I reference latest workbook in series?

  • Thread starter Thread starter SidBord
  • Start date Start date
S

SidBord

It sounds like you could make use of a "static" variable.
This is kinda clutzy, but it should work. In the module
that contains all macros that might need to reference the
last workbook, enter "Dim LastWkBkNo As Integer" at the TOP
of the module BEFORE the first Sub statement. That should
make the variable available to all procedures. You can
store whatever you want to in it, but it sounds like the
value of "i" might be a good choice. Or make it a string
variable and store the full name of the workbook.
 
I think I liked your first routine better--just in case some file was deleted
(via window explorer???).

But if you could trust the date/time on the file, you could use .filesearch to
pick off the newest file:


With Application.FileSearch
.NewSearch
'Change this to your directory
.LookIn = "C:\my documents\excel"
.Filename = "analysis *.xls"
.SearchSubFolders = False
If .Execute(msoSortByLastModified, msoSortOrderDescending) > 0 Then
MsgBox .FoundFiles(1) & vbLf & FileDateTime(.FoundFiles(1))
End If
End With

And then split it up and extract the last number used.
 
ps. I use the date/time in the workbook name. I get the same backup, and it's
pretty much unique just by having a few ticks go by on the clock.

stFileName = FolderName & "Analysis _" & format(now,"yyyymmdd_hhmmss") & ".xls"

it makes life easier when cleaning up, too.
(Named _20010612_132254.xls???? It's old, just dump it!)
 

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

Back
Top