Combine multiple CSV files automatically instead of using Application.GetOpenFilename

davidp13

New Member
Joined
Jun 7, 2011
Messages
25
Hi. I have a piece of code that helps me select multiple CSV files and then import them into one sheet where I will then run additional scripts to build output data. I'm looking to automate the process more and trying to circumvent the selection of the CSV through the manual process and rather have the script automatically pick up all the files and then import + manipulate as needed.

Below is the code. Can anyone help me change this so that when I execute it, it will choose all the CSV in a folder automatically? Thanks

Code:
Sub Import()    Dim fn, ws As Worksheet, e, flg As Boolean, LastR As Range, wsName As String
   
    fn = Application.GetOpenFilename("Excel(*.csv*),*.csv*", MultiSelect:=True)
    
    If Not IsArray(fn) Then Exit Sub
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Application.ScreenUpdating = False
    ws.Cells.Clear
    For Each e In fn
        With Workbooks.Open(e)
            With .Sheets(1)
                wsName = .Name
                If Not flg Then
                    .Rows(1).Copy ws.Cells(1)
                    ws.Columns(1).Insert
                    ws.Cells(19).Value = "Sheet name"
                    flg = True
                End If
                Set LastR = ws.Cells(Rows.Count, 2).End(xlUp)(2)
                With .Range("a1").CurrentRegion
                    With .Resize(.Rows.Count - 1).Offset(1)
                        .Copy LastR
                        LastR(, 18).Resize(.Rows.Count).Value = _
                            CreateObject("Scripting.FileSystemObject").GetBasename(e)
                    End With
                End With
            End With
            .Close False
        End With
    Next
    Sheets("Sheet1").Range("A:A").EntireColumn.Delete
    ws.Range("a1").CurrentRegion.Columns.AutoFit
    Application.ScreenUpdating = True
    Set ws = Nothing
End Sub
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Post your current code, and we will see if we can help you add that piece in.
 
Upvote 0
Thanks Joe4. Below is the code I have now and I only need the add the CSV filename on each row.

Code:
Option Explicit

Sub ImportCSV()


    Dim strSourcePath As String
    Dim strDestPath As String
    Dim strFile As String
    Dim strData As String
    Dim x As Variant
    Dim Cnt As Long
    Dim r As Long
    Dim c As Long
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Sheets("Sheet1")
    Application.ScreenUpdating = False
    ws.Cells.Clear
    
    'Change the path to the source folder accordingly
    strSourcePath = "C:\Users\admi n\Google Drive\Everest\Daily Scans - Portfolio_v2\"
    
    If Right(strSourcePath, 1) <> "\" Then strSourcePath = strSourcePath & "\"
    
    'Change the path to the destination folder accordingly
    strDestPath = "C:\Users\admi n\Google Drive\Everest\Daily Scans - Portfolio_v2\"
    
    If Right(strDestPath, 1) <> "\" Then strDestPath = strDestPath & "\"
    
    strFile = Dir(strSourcePath & "*.csv")
    
    Do While Len(strFile) > 0
        Cnt = Cnt + 1
        If Cnt = 1 Then
            r = 1
        Else
            r = Cells(Rows.Count, "A").End(xlUp).Row + 1
        End If
        Open strSourcePath & strFile For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
            If Cnt > 1 Then
                Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strData
                
            End If
            Do Until EOF(1)
                Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , strData
                x = Split(strData, ",")
                For c = 0 To UBound(x)
                    Cells(r, c + 1).Value = Trim(x(c))
                    ws.Cells(18).Value = "Sheet name"
                Next c
                r = r + 1
            Loop
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Name strSourcePath & strFile As strDestPath & strFile
        strFile = Dir
    Loop
    
    Application.ScreenUpdating = True
    
    If Cnt = 0 Then _
        MsgBox "No CSV files were found...", vbExclamation
    
End Sub
 
Upvote 0
See if changing this file:
Code:
ws.Cells(18).Value = "Sheet name"
to this:
Code:
ws.Cells(r,18).Value = strFile
does what you want.
 
Upvote 0
Thanks a mil again for your help it is really appreciated. The code below is what worked.

Code:
ws.Cells(r, 18).Value = CreateObject("Scripting.FileSystemObject").GetBasename(strFile)
 
Upvote 0
You are welcome.

I am curious, did the solution I provided with just using "strFile" not work for you?
It did for me in testing?
If it does, it may be a little more efficient than creating another object.
 
Upvote 0
It copies the .csv part of the filename as well and I only want the name excluding the suffix. the object way is a bit slower so i'm open to any suggestions it more efficient. Thanks
 
Upvote 0
This will do it:
Code:
ws.Cells(r,18).Value = Left(strFile,Len(strFile)-4)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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