PandasPython
PandasPython

Reputation: 3

Importing CSV Files into Excel based on reference list

I have an excel file which I have built to import all csv files from a given date (entered in by the user using a message box. This all works fine and as expected.

The change I would like to make is to only import the csv files which start with the names given in a specific reference list. For example...

File Names:

Reference list stored in main spreadsheet would be as follows:

In this scenario I would want the code to import all files which start with File1 & File3 and ignore the rest.

I have the following so far for my csv import but as I say, it imports everything within that folder.

    Public Sub CombineFiles()

    i = 0
    
    myValue = InputBox("How many days do you wish to import data from? (0 = Today)", "Date Rollback", 0)
'inform user
    If Not IsNumeric(myValue) Then
        MsgBox "Numeric Values Only", 48, "Numeric Values Only"
        Exit Sub
    Else
    
    Sheets("Dashboard").Range("K3").Value = "LOADING"
    
    Sheets("ImportedData").Cells.Clear
    Sheets("DuplicateRecords").Cells.Clear
    
    ThisWorkbook.Activate
    ThisWorkbook.Worksheets("ImportedData").Select

    Set S = Worksheets("ImportedData")
    
    boolGetHeaders = True

    strPath = "folderpath" & Format(DateAdd("d", -myValue, CDate(Date)), "yyyy-MM-dd") & "\"
    If Dir$(strPath) = "" Then
    MsgBox "Folder not found - " & strPath, 48, "Folder Not Found"
    Exit Sub
    End If
    FindFilesFromFolders strPath
    
End If

With ActiveWorkbook.Worksheets("NameList")

LastRowColumnA = .Range("A" & Rows.Count).End(xlUp).Row

For Each myCell In .Range("A2:A" & LastRowColumnA)

    strFile = strPath & myCell.Value & "-*.csv"

    If Len(Dir(strFile)) > 0 Then

        Sheets.Add Type:=strFile, After:=Worksheets(Worksheets.Count)
        
        FindFilesFromFolders strPath

    Else: myCell.Offset(0, 7) = "File Not Found"
    End If

Next

End With

End Sub

Upvotes: 0

Views: 43

Answers (1)

CHill60
CHill60

Reputation: 1958

Just test for that condition before importing the files e.g.

If Left(myCell.Value, 4) = "File1" Or Left(myCell.Value, 4) = "File3" Then

    strFile = strPath & myCell.Value & "-*.csv"

    If Len(Dir(strFile)) > 0 Then

        Sheets.Add Type:=strFile, After:=Worksheets(Worksheets.Count)
        
        FindFilesFromFolders strPath

    Else
        myCell.Offset(0, 7) = "File Not Found"
    End If
End If

Upvotes: 0

Related Questions