Import *.csv formatted multiple files from folder in to excel

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>

Hi,</SPAN></SPAN>

I have created one folder on desktop and named "csvfiles" when I receive a new file I just add into this folder (in the same folder I have put one excel workbook named "csvfilestoexcel" </SPAN></SPAN>

I want I code when I open "csvfilestoexcel" and run the macro it list all the files are in to the folder "csvfilestoexcel" in the sheet1 into the column A and then all "csvfiles" are listed in the sheet1 import them one by one in the sheet "Imported"</SPAN></SPAN>

Is it possible?</SPAN></SPAN>

This is code I have recorded and imported 2 columns data from 2 files a1 & a2 and paste them transpose in to sheet "Imported" need a code which can do import all files automatically </SPAN></SPAN>
Code:
Sub Import_Csvfiles()
    Sheets("Imported").Select
    ChDir "D:\Desktop\csvfiles"
    Workbooks.Open Filename:="D:\Desktop\csvfiles\a1.csv"
    Range("C1:C14").Select
    Selection.Copy
    Windows("csvfilestoexcel.xls").Activate
    Range("D10").Select
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("S10").Select
    Windows("a1.csv").Activate
    Range("D1:D14").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("csvfilestoexcel.xls").Activate
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("D11").Select
    Windows("a1.csv").Activate
    ActiveWindow.Close
    
    
    Workbooks.Open Filename:="D:\Desktop\csvfiles\a2.csv"
    Range("C1:C14").Select
    Selection.Copy
    Windows("csvfilestoexcel.xls").Activate
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("S11").Select
    Windows("a2.csv").Activate
    Range("D1:D14").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("csvfilestoexcel.xls").Activate
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("D9").Select
    Windows("a2.csv").Activate
    ActiveWindow.Close
    ActiveWorkbook.Save

End Sub
</SPAN></SPAN>

Example of imported files</SPAN></SPAN>


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
2
3
4
5
6
7
8
9
1046,346,89056,626,630,955,331,837,14359,643,857,565,428,928,47,4425,525,830,227,132,932,331,526,529,727,824,1
1132,54,4630,755,210,438,259,128,740,458,335,160,153,530,62910,829,224,417,729,225,532,933,22731,327,42732,2
12
13
14
Imported


Thank you in advance</SPAN></SPAN>

Regards,</SPAN>
Kishan</SPAN>
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this

Adjust the data in blue with your information.

Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    
    Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Sheets("[COLOR=#0000ff]Imported[/COLOR]")
    
    wPath = "[COLOR=#0000ff]D:\Desktop\csvfiles\[/COLOR]"
[COLOR=#0000ff]    i = 10[/COLOR]
    arch = Dir(wPath & "*.csv")
    Do While arch <> ""
        Set wb2 = Workbooks.Open(Filename:=wPath & arch)
        Set sh2 = wb2.Sheets(1)
        sh2.Range("[COLOR=#0000ff]C1:C14[/COLOR]").Copy
        sh1.Cells(i, "[COLOR=#0000ff]D[/COLOR]").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        i = i + 1
        wb2.Close False
        arch = Dir()
    Loop
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
 
Upvote 0
Try this

Adjust the data in blue with your information.

Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    
       Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
DanteAmor, thank you for the macro it copy only one column, which is perfect! :) Later I will see if I can modify to copy both.<o:p></o:p>
<o:p></o:p>
I need please can you add couples of things
<o:p></o:p>
One for example if I put folder "csvfiles" in to other folder is it possible the path could be allocated where ever if file find auto path.
<o:p></o:p>
<o:p></o:p>
Second is it possible list folder files by there older to latest time and dates in the column A in to sheet1 says for example
<o:p></o:p>
<o:p></o:p>
Kind regards,
<o:p></o:p>
Kishan
 
Upvote 0
Change this

Code:
wPath = "D:\Desktop\csvfiles\"

By:

Code:
wPath = [COLOR=#333333]wb1.path & "\"[/COLOR]
 
Upvote 0
Change this

Code:
wPath = "D:\Desktop\csvfiles\"

By:

Code:
wPath = [COLOR=#333333]wb1.path & "\"[/COLOR]
DanteAmor, this change worked spot on now no problem I can put folder where I want the code detect path automatically very clever. </SPAN></SPAN>

I tried to incorporate modification inside the code so it can copy the Range("D1:D14") starting from S10 but could not get it work.
</SPAN></SPAN>

May I have duplicate the code and run first one and then second to do copy both as per post#1?
</SPAN></SPAN>

Thank you so much for you help
</SPAN></SPAN>

Kind regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Try this

Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    
    Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Sheets("Imported")
    
    wPath = "D:\Desktop\csvfiles\"
    i = 10
    arch = Dir(wPath & "*.csv")
    Do While arch <> ""
        Set wb2 = Workbooks.Open(Filename:=wPath & arch)
        Set sh2 = wb2.Sheets(1)
        sh2.Range("C1:C14").Copy
        sh1.Cells(i, "D").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        sh2.Range("D1:D14").Copy
        sh1.Cells(i, "S").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True


        i = i + 1
        wb2.Close False
        arch = Dir()
    Loop
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
 
Upvote 0
Try this

Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    
    Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Sheets("Imported")
    
    wPath = "D:\Desktop\csvfiles\"
    i = 10
    arch = Dir(wPath & "*.csv")
    Do While arch <> ""
        Set wb2 = Workbooks.Open(Filename:=wPath & arch)
        Set sh2 = wb2.Sheets(1)
        sh2.Range("C1:C14").Copy
        sh1.Cells(i, "D").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        sh2.Range("D1:D14").Copy
        sh1.Cells(i, "S").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True


        i = i + 1
        wb2.Close False
        arch = Dir()
    Loop
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
Hi DanteAmor,

Thank you for the code, it worked perfectly as per my request.

What I want now are 2 things additional to the above, if possible;
1) is for the files to open and update data by order; from older hours/date to latest.
2) the name of the worksheet to update in column C, next to the data. For example a1.csv in cell C10 and a2.csv in cell C11

Kind Regards,
Kishan
 
Upvote 0
I put the name in column B and the last update date in column C, with that you can now order the records from highest to lowest or from lowest to highest.


Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    Dim fso As Variant, carpeta As Variant, ficheros As Variant, archivo As Variant
    
    Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook
    Set sh1 = wb1.Sheets("Imported")
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    wPath = wb1.Path & "\"
    
    Set carpeta = fso.GetFolder(wPath)
    Set ficheros = carpeta.Files
    
    i = 10
    'arch = Dir(wPath & "*.csv")
    'Do While arch <> ""
    For Each archivo In ficheros
        If LCase(Right(archivo, 3)) = "csv" Then
            Set wb2 = Workbooks.Open(Filename:=archivo)
            Set sh2 = wb2.Sheets(1)
            sh2.Range("C1:C14").Copy
            sh1.Cells(i, "D").PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
            sh1.Cells(i, "B").Value = archivo.Name
            sh1.Cells(i, "C").Value = archivo.DateLastModified
            i = i + 1
            wb2.Close False
            'arch = Dir()
        End If
    Next
    'Loop
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
 
Upvote 1
Solution
I put the name in column B and the last update date in column C, with that you can now order the records from highest to lowest or from lowest to highest.


Code:
Sub Import_Csvfiles()
    Dim sh1 As Worksheet, sh2 As Worksheet, wb1 As Workbook, wb2 As Workbook
    Dim wPath As String, arch As Variant, i As Long
    Dim fso As Variant, carpeta As Variant, ficheros As Variant, archivo As Variant
    
           End If
    Next
    'Loop
    Application.ScreenUpdating = True
    MsgBox "Done"
End Sub
Hi DanteAmor,</SPAN></SPAN>

Thank you so much I like the column B&C updating idea </SPAN></SPAN>

here is the resulting data where the a1 is the older and a21 is the latest. and if I sort them it remain the same. would not it be good that VBA import a1..to...a21.... </SPAN></SPAN>

Result data...</SPAN>
</SPAN>

Book1
ABCDEFGHIJKLMNOPQRS
1
2
3
4
5
6
7
8
9
10a1.csv22/04/2019 12:4010162,32156,512210316914780,793,5397184342135
11a10.csv22/04/2019 12:4128660,223918611917615625835,215910723432429,2
12a11.csv22/04/2019 12:4137217786,630067,234233018618538510324594,232,4
13a12.csv22/04/2019 12:4128330424323111,17,1217630059,4246218324121320
14a2.csv22/04/2019 12:4061,219692,43345618724436893,698,199,118239587,7
15a20.csv22/04/2019 12:4128727269,617412922524,396,129,51363283,34190324
16a21.csv22/04/2019 12:4136970,235527632525038061,1354390347204223114
17a3.csv22/04/2019 12:407,1955,335133716026375,72621042441,7824,2228260
18a4.csv22/04/2019 12:403681717432411415293,934864,338637236438,8114
19a5.csv22/04/2019 12:402411892882443138,12426,4115,636115225354,5272
20a6.csv22/04/2019 12:4034622414768,614511128229127318713023490,664,4
21a7.csv22/04/2019 12:412783491411373473433604,6384,431616982,7260194
22a8.csv22/04/2019 12:4112820216526354,33081771041681,8126836,2330152
23a9.csv22/04/2019 12:4123979,6314118162127297382344268332266168292
24
25
Imported


Kind Regards,</SPAN></SPAN>
Kishan</SPAN></SPAN>
 
Upvote 0
Vba imports them as they are in the folder, for that you have the columns, to sort by date, check that it really is a date and not a text
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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