VBA Excel: finding needed name of the file

Let say there is two databases which belong to the same data-set and have names PEP 29 05 15 09.txt and PEP.txt

So the properties of the names always the same :
1. Name start from the alphabetic part
2. Alphabetic part contain from 1 to 5 letters
3. Number part on the 2 data bases is always different or absent

I need to write application that, in the folder, detect two databases which belong to the same data-set and import the content of the bases in the spreadsheet.  

This is the code that upload data from the file to spreadsheet

Sub ImportFile()
Dim fd As FileDialog
Dim strFile As String
Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim lngNewRow As Long
Dim intParts As Integer

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .Filters.Clear
    .Filters.Add "Text files", "*.txt"
    .Filters.Add "All files", "*.*"
    If .Show = -1 Then
        strFile = .SelectedItems(1)
    Else
        MsgBox "Cancel selected"
        Exit Sub
    End If
End With
Set fd = Nothing

FF = FreeFile
Open strFile For Input As #FF

Do While Not EOF(FF)
    Line Input #FF, strLine
    strParts = Split(strLine, vbTab)
    lngNewRow = lngNewRow + 1
    With ActiveSheet
        For intParts = 0 To UBound(strParts)
            .Cells(lngNewRow, intParts + 1) = strParts(intParts)
        Next
    End With
Loop
Close
End Sub

Open in new window


In the attachment you can find the example of the files.

Many thx in advance !
PEP.txt
PEP-29-05-15-09.txt
SunnyXAsked:
Who is Participating?
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.

Martin LissOlder than dirtCommented:
This will allow you to choose two files (actually any number of files) and process them at the same time. The "Prefix" it asks for would be something like "PEP".

Sub ImportFile()
Dim fd As FileDialog
Dim strFile As String
Dim FF As Integer
Dim strLine As String
Dim strParts() As String
Dim lngNewRow As Long
Dim intParts As Integer
Dim intCount As Integer
Dim strPrefix As String


strPrefix = InputBox("Please enter file name prefix")
If strPrefix = "" Then
    MsgBox "No prefix selected"
    Exit Sub
End If

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .Filters.Clear
    .Filters.Add "Text files", "*.txt"
    .Filters.Add "All files", "*.*"
    .InitialFileName = strPrefix & "*.txt"
    .AllowMultiSelect = True
    If .Show = -1 Then
        For intCount = 1 To .SelectedItems.Count
            strFile = .SelectedItems(intCount)
            FF = FreeFile
            Open strFile For Input As #FF
            
            Do While Not EOF(FF)
                Line Input #FF, strLine
                strParts = Split(strLine, vbTab)
                lngNewRow = lngNewRow + 1
                With ActiveSheet
                    For intParts = 0 To UBound(strParts)
                        .Cells(lngNewRow, intParts + 1) = strParts(intParts)
                    Next
                End With
            Loop
            Close
        Next
    Else
        MsgBox "Cancel selected"
        Exit Sub
    End If
End With
Set fd = Nothing

End Sub

Open in new window

0

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
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
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
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.