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

Posted on 2006-05-10
Last Modified: 2010-04-07
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?
Question by:Prioritypub
    LVL 35

    Accepted Solution


    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")

    LVL 35

    Expert Comment

    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


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now