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: 22

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What is in column A? Already there to start with?
 
Upvote 0
What is in column A? Already there to start with?
Thanks Zot. All csv files have Column A and Column B. And Column A is the same in all csv files. I want to copy at least one Column A from one of the csv files say file 1.csv and all column b from all the csv files will be lined up next to each other in one worksheet. file names can be used as headers of the column. thank you
 
Upvote 0
Should be easier to help with at least some attachments and a complete explanation so without anything to guess …​
 
Upvote 0
Should be easier to help with at least some attachments and a complete explanation so without anything to guess …​
I don't know how to attach excel files but here's a snapshot of what my csv file looks like. I just want to compile all Column B next to each other into a single worksheet. I do not need to copy Coulmn A as they will be the same in all files. I want to have a final output as with the attached file :) Thanks all!
 

Attachments

  • file 2.JPG
    file 2.JPG
    64.7 KB · Views: 9
  • file1.JPG
    file1.JPG
    82.5 KB · Views: 10
  • file3.JPG
    file3.JPG
    83.7 KB · Views: 10
  • output.JPG
    output.JPG
    221.6 KB · Views: 9
Upvote 0
Install this macro in normal module in a fresh workbook. Once macro is run, it will ask to select a folder where you put all the files you wanted to copy from. Make sure you click on the folder until you see the Folder Name appears in the box and click OK.
Note: Make sure the folder you select only contains only the source file you wanted to copy. The program will loop all the files in there.
VBA Code:
Option Explicit

Sub GetLastRow()

Dim SelectFolder As Integer
Dim x As Long, xx As Long
Dim strPath As String
Dim wsSummary As Worksheet
Dim wb As Workbook
Dim FSOLibrary As FileSystemObject
Dim FSOFolder As Object
Dim sFileName As Object

Set wsSummary = 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 = 1
xx = 2
'Loop through each file in a folder
For Each sFileName In FSOFolder.Files
    Set wb = Workbooks.Open(sFileName)
    wb.Sheets("Sheet1").Range("A1", wb.Sheets("Sheet1").Cells(Rows.Count, xx).End(xlUp)).Copy wsSummary.Cells(1, x)
    If x = 1 Then
        x = x + 1
        xx = xx - 1
    End If
    x = x + 1
    wb.Close True
Next

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

End Sub
 
Upvote 0
Install this macro in normal module in a fresh workbook. Once macro is run, it will ask to select a folder where you put all the files you wanted to copy from. Make sure you click on the folder until you see the Folder Name appears in the box and click OK.
Note: Make sure the folder you select only contains only the source file you wanted to copy. The program will loop all the files in there.
VBA Code:
Option Explicit

Sub GetLastRow()

Dim SelectFolder As Integer
Dim x As Long, xx As Long
Dim strPath As String
Dim wsSummary As Worksheet
Dim wb As Workbook
Dim FSOLibrary As FileSystemObject
Dim FSOFolder As Object
Dim sFileName As Object

Set wsSummary = 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 = 1
xx = 2
'Loop through each file in a folder
For Each sFileName In FSOFolder.Files
    Set wb = Workbooks.Open(sFileName)
    wb.Sheets("Sheet1").Range("A1", wb.Sheets("Sheet1").Cells(Rows.Count, xx).End(xlUp)).Copy wsSummary.Cells(1, x)
    If x = 1 Then
        x = x + 1
        xx = xx - 1
    End If
    x = x + 1
    wb.Close True
Next

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

End Sub


Thanks, Zot. I tried to run the macro but gave me this error :( Sorry I am a newbie to coding.
1619667875200.png
 
Upvote 0
I think you need to go VB Editor. Go to
Tools > References and then tick Microsoft Scripting Runtime.
 
Upvote 0
Install this macro in normal module in a fresh workbook. Once macro is run, it will ask to select a folder where you put all the files you wanted to copy from. Make sure you click on the folder until you see the Folder Name appears in the box and click OK.
Note: Make sure the folder you select only contains only the source file you wanted to copy. The program will loop all the files in there.
VBA Code:
Option Explicit

Sub GetLastRow()

Dim SelectFolder As Integer
Dim x As Long, xx As Long
Dim strPath As String
Dim wsSummary As Worksheet
Dim wb As Workbook
Dim FSOLibrary As FileSystemObject
Dim FSOFolder As Object
Dim sFileName As Object

Set wsSummary = 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 = 1
xx = 2
'Loop through each file in a folder
For Each sFileName In FSOFolder.Files
    Set wb = Workbooks.Open(sFileName)
    wb.Sheets("Sheet1").Range("A1", wb.Sheets("Sheet1").Cells(Rows.Count, xx).End(xlUp)).Copy wsSummary.Cells(1, x)
    If x = 1 Then
        x = x + 1
        xx = xx - 1
    End If
    x = x + 1
    wb.Close True
Next

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

End Sub

Hi Zot

I was able to fix the error but when I tired to run the macro it copied Column A of each csv files instead.. See Columns C-G below..... It was supposed to copy the Column B of each files :(
1619670735205.png
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,936
Members
449,094
Latest member
teemeren

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