VBA: Import folder contents, search for row/header copy file name to column

hennesseeb

New Member
Joined
Dec 30, 2016
Messages
12
Hello,

I've got a macro built that imports .csv files and copies a range of data into a destination workbook. What I need it to do is search the .csv files for certain header/row criteria and paste it into the destination workbook. See below for current code.

Each row that I need will start with 1,2,3...etc and span until column I. The number of rows will vary from file to file, so a search function is needed. The header names will stay the same throughout each file. Also, I will need the file name to populate column for each corresponding entry.

I would like this code so I don't have to manually open hundreds of files just to copy and paste data. I appreciate the help. I'm new here at Mr. Excel forums.


Code:
Sub ImportFolder()
    MsgBox "Select the folder you want to import."
    
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    With fd
        If .Show = -1 Then
            folder = .SelectedItems(1)
        Else
            GoTo CANCEL
        End If
    End With
    Application.ScreenUpdating = False
    Dim StrFile As String
    StrFile = Dir(folder & "\*.csv")
    Do While Len(StrFile) > 0
        fandp = folder & "\" & StrFile
        
        Workbooks.Open Filename:=fandp
        Range("A8:I10").Select
        Selection.Copy
        Workbooks("File Import.xlsm").Activate
        Rows("2:2").Select
        Selection.Insert Shift:=xlDown
        Application.CutCopyMode = False
        c = Application.CountA(Sheets("Data").Range("A:A"))
        ActiveSheet.Range("$A$1:$I$" & c).RemoveDuplicates Columns:=1, Header:=xlYes
        
        Windows(StrFile).Close (False)
        StrFile = Dir
        Start = ""
 

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top