anastasia1428
New Member
- Joined
- Apr 26, 2021
- Messages
- 6
- Office Version
- 365
- Platform
- Windows
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 youWhat is in column A? Already there to start with?
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!Should be easier to help with at least some attachments and a complete explanation so without anything to guess …
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
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
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