A macro command that will open an unknown file in a known folder?

I have an Excel file in a folder with a name that will often change. I need to open that file and copy contents from it to another Excel file. I have the macro written to perform all the tasks, except if I don't know what the name of the file will be when the macro is run, so how can I open it?  It will be the only file in this folder, and the folder name will never change.  Is there a command line that will rename the file (without knowing what that file name is) if I know the folder name?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


The Dir() function returns filenames from a specified path.  You could use that to return the file name.  Since you said there will only be one file, I've written this to only return the first file found.  If no files are found, it will return a zero-length string:

Function FirstFileName(ByVal ThePath As String) As String
 If Right(ThePath, 1) <> "\" Then ThePath = ThePath & "\"
 FirstFileName = Dir(ThePath)
End Function

If there could be non-xls files there, you can narrow it down to only look for .xls files:

 FirstFileName = Dir(ThePath & "*.xls")


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry, I should have given you a sample usage of this:

 Dim vFile As String
 vFile = FirstFileName("C:\foldername")
 If Len(vFile) > 0 Then
  Workbooks.Open vFile
 End If

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.