Name WB Via Newest File

JLouis

Active Member
Joined
Jan 1, 2004
Messages
295
Office Version
  1. 365
Platform
  1. Windows
So I do a seach in a directory to find the newest file that has been downloaded into the directory. Once that file has been identified, it is opened and processed into my workbook. If for some reason I download an incorrect file into the directory, it obviously screws with my process workbook. (Just happened) If I can identify an incorrect file before processing I can stop the code from finishing.

I'd like to be able to name this newest file before processing so I can run a test to see if it's the correct file file. I have another macro that processes the opened WB. If I have a name for this opened file, I can reference it in my "TestMe" macro. I may need to name it before opening, but after it has been identified as the newewst file.

Finally, maybe I have the wrong idea completely and would welcome a different approach. Thanks so much for all the help you folks provide.

VBA Code:
    Do While Len(MyFile) > 0
        LMD = FileDateTime(MyPath & MyFile)
       If LMD > LatestDate = 0 Then GoTo finish
        If LMD > LatestDate  > 0 Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        MyFile = Dir
    Loop
    Workbooks.Open MyPath & LatestFile
   ' TestMe Newest file is opened waiting to be used. I'd like to test it before processing.
    ActiveWorkbook.Activate
    Exit Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I would use the file name as a way of identifying what is in the file.

The file name can have the date and time embedded within it but having an index number within the filename
would enable you to clearly identify which one is the latest.
 
Upvote 0
I would use the file name as a way of identifying what is in the file.

The file name can have the date and time embedded within it but having an index number within the filename
would enable you to clearly identify which one is the latest.

I agree - seems like a good idea.
 
Upvote 0
Hello and thank you for the responses. I was afraid I didn't explain it very well.

When the file is found, it has a naming convention that changes each time a new file is downloaded. It has a different name each time. Once identified by date, it opens for processing. What I need is a method to name this file "New File.csv" as an example. Then I can refer to that file in another file for testing it's the right kind of file. Does that help?
 
Upvote 0
I fixed the problem. No harder than adding:

VBA Code:
   Loop
    Workbooks.Open MyPath & LatestFile
   ' TestMe
   
   ActiveWorkbook.SaveAs MyPath & "NewFile.csv"
    ActiveWorkbook.Close
    Exit Sub

I can now reference the new file in my test macro. Much ado about nothing.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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