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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I did not try but I think this should work
VBA Code:
Sub GetLastRow()

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

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

End Sub
 
Upvote 0
I did not try but I think this should work
VBA Code:
Sub GetLastRow()

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

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

End Sub
Yesss it worked!! Thank you so much :):):)
 
Upvote 0
Glad to be able to help. Nothing to do in office now ?
I know I'm replying to a really old thread, but I am trying to do the exact same thing as OP. However, when I run this code I get the following error message:
1727890863383.png


I'm hoping you're still around and can help out. Any input is appreciated!
 
Upvote 0
I know I'm replying to a really old thread, but I am trying to do the exact same thing as OP. However, when I run this code I get the following error message:
View attachment 117606

I'm hoping you're still around and can help out. Any input is appreciated!
Did you look at posts 8 and 9 in this thread, and ensure that you have selected the appropriate libraries in order to use this code?
 
Upvote 0
Did you look at posts 8 and 9 in this thread, and ensure that you have selected the appropriate libraries in order to use this code?
Yes I enabled those libraries, and the code runs, but it will only "scan-in" one .CSV file from my csv folder before it throws up this error code.
 
Upvote 0
Which line of code is it returning the error on?
If you are not sure, try stepping into your code and using the F5 to run the code one line at a time, until the error occurs, and then see which line of code it is on.
 
Upvote 0
Which line of code is it returning the error on?
If you are not sure, try stepping into your code and using the F5 to run the code one line at a time, until the error occurs, and then see which line of code it is on.
It's throwing up the error on this highlighted line
1727892408510.png
 
Upvote 0
That doesn't seem to make sense. If the first one worked, it should have incremented x to 2 at that point, so it should not be hitting that line anymore.

Are your sure the first one is working?

Also, are you sure that your sheet names match (i.e. "Sheet1")?

And, are if you are failing on the second record, are you sure that the file name listed for the second file is correct?
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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