Importing Based on a Partial File Name

Ira receives a weekly text-delimited file that he imports into Excel by using a macro. The file name was always the same each week, so Ira was able to embed it into the macro; this made importing very easy. Recently the vendor’s new system started changing the file name each week, but the first 12 characters always remain the same. Ira wonders how he can code the macro to import this text file without needing to modify the file name each week.

There are a few ways you can approach this problem, and all of them may mean some extensive recoding of your macro. The method you choose should depend on how you want to do your work each week. For instance, let’s say that you know the directory in which the incoming file is stored, what the base 12 characters are for the filename, and the filename extension. With that information, you could simply prompt the user to provide the suffix for the filename, in this manner:

Sub OpenImportFile()
    Dim sFileName As String
    Dim sBase As String
    Dim sSuffix As String
    Dim sExt As String

    sBase = "c:MyDirectoryFirst12Chars"
    sExt = ".csv"
    sSuffix = InputBox("Enter suffix for filename")

    sFileName = sBase & sSuffix & sExt
    Workbooks.Open Filename:=sFileName
End Sub

You can, obviously, change the base and extension as you desire. This example assumes that you are importing a CSV file, which should open in Excel just fine.

If you are simply placing a group of files into a folder and you want to open all of them, then the code becomes a bit quicker to run because you don’t need to get user input.

Sub OpenImportFiles()
    Dim sFileName As String
    Dim sBase As String
    Dim sExt As String

    sBase = "c:MyDirectoryFirst12Chars"
    sExt = ".csv"

    sFileName = Dir(sBase & "*" & sExt)
    If sFileName = "" Then
        MsgBox "No Files Found"
    Else
        Do While sFileName > ""
            Workbooks.Open Filename:=sFileName
            sFileName = Dir
        Loop
    End If
End Sub

This approach opens all the CSV files in the folder; to open a different type of file, just change what is stored in the sExt variable. One thing to remember is that since all the CSV files in the folder are opened, that means it may open import files from previous weeks. (This may or may not be what you want.)

Everything presented in this tip so far assumes that when you “import” a file, all you want to do is open the file so you can work with it. That may not be the case; you may want your macro to process the import file in some way and do something with it. That functionality may already be in your older macro, so all you need to do is change how the import file is identified. If this is the case, the code you use can be adapted from the code already presented in this tip.

Another way to grab the data out of a text file and stuff it into a workbook is to use the OpenText method, as shown in this example code.:

Sub OpenImportFile()
    Dim sFileName As String
    Dim sBase As String
    Dim sExt As String

    sBase = "c:MyDirectoryFirst12Chars"
    sExt = ".csv"
    sFileName = sBase & "*" & ".txt"

    Workbooks.OpenText Filename:=sFileName, Origin:= _
      xlMSDOS, StartRow:=1, DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, Tab:=True, _
      Semicolon:=False, Comma:= False, Space:=False, _
      Other:=False, FieldInfo:=Array(1, 1), _
      TrailingMinusNumbers:=True
End Sub

A full exploration of what the OpenText method does is beyond the scope of this tip, but the result is that all of the delimited text files that begin with the base 12 characters should be opened in your workbooks. You can modify how the method works by changing the parameters. You’ll want to pay particular attention to the parameters used to specify what delimiter character should be used.