Import multiple csv files into separate columns in a worksheet

anastasia1428

New Member
Joined
Apr 26, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have 54 csv files and I want to import into a single worksheet. All files have the same values for column A.
So I want to make a summary that looks like this. Can you help me with the code please?
1619496529124.png
 

Attachments

  • 1619496416399.png
    1619496416399.png
    70.1 KB · Views: 33

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Yes, the code compiles without errors.

Ok here's the two test files I'm using
I've been absent for quite long in this forum. I need to understand what I was doing and I also got confused myself 😁
I re-write the program. This time no need to set library upfront because it will be set automatically during runtime.

VBA Code:
Option Explicit

Sub GetCSVData()

Dim SelectFolder As Integer
Dim x As Long, rowLast As Long
Dim strPath As String
Dim FirstFile As Boolean
Dim wbSummary As Workbook, wb As Workbook
Dim wsSummary As Worksheet, ws As Worksheet
Dim FSO As Object
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim sFileName As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set wbSummary = ActiveWorkbook
Set wsSummary = wbSummary.Sheets("Sheet1")
SelectFolder = Application.FileDialog(msoFileDialogFolderPicker).Show

If Not SelectFolder = 0 Then
    strPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
Else
    End
End If

Application.ScreenUpdating = False
'Set all the references to the FSO Library
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSOLibrary.GetFolder(strPath)

x = 2
FirstFile = True
'Loop through each file in a folder
For Each sFileName In FSOFolder.Files
    Set wb = Workbooks.Open(sFileName)
    Set ws = wb.Sheets(1)
    rowLast = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
    If FirstFile Then
        ws.Range("A1", "B" & rowLast).Copy wsSummary.Range("A2")
        FirstFile = False
    Else
        x = x + 1
        ws.Range("B1", "B" & rowLast).Copy wsSummary.Cells(2, x)
    End If
    wsSummary.Cells(1, x) = Dir(sFileName)
    wb.Close True
Next

Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Application.ScreenUpdating = True

End Sub

I tried with sample you provided. Unlike previous one, this will write the file name on first row starting from column B.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,635
Members
452,661
Latest member
Nonhle

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