Excel macro to copy data from same column of different files within a folder to a single Excel

denniswong95

New Member
Joined
Oct 23, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I have a folder containing thousands of files with similar data from sensor. (1 machine cycle will generate 1 csv file.)
The sensor data is stored in column B in every files.
I would like to copy all the column B data to a single file to do analysis.

The first thing when running the macro is to locate the folder.
Then the macro will run through all the files and copy the data stored in column B to a new worksheet.
The first column copied is to be placed in Column B of the new worksheet whereas subsequent data is to be placed in following column C, D E and etc accordingly until all files have been run through within the folder.

Anyone can help on this would be great.

Dennis
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Place the macro in a new, blank workbook. Change the destination sheet name (in red) and the source sheet name (in blue) to suit your needs.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim wsDest As Worksheet, wkbSource As Workbook, FolderName As String, bottomB As Long
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       FolderName = .SelectedItems(1) & "\"
    End With
    ChDir FolderName
    strExtension = Dir("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(FolderName & strExtension)
        With Sheets("Sheet1")
            bottomB = .Range("B" & .Rows.Count).End(xlUp).Row
            wsDest.Cells(1, wsDest.Columns.Count).End(xlToLeft).Offset(0, 1).Resize(bottomB).Value = .Range("B1:B" & bottomB).Value
        End With
        wkbSource.Close False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps, I tried to copy it to a blank sheet but it does not work at all.
I suspect it is because my file format is in csv file instead of xlsx file.
Another thing is that for every file, the sheet name is auto generated therefore it is different for every file.
Please refer to the image uploaded for your reference.
1698109620938.png
 
Upvote 0
Place the macro in a new, blank workbook. Change the destination sheet name (in red) and the source sheet name (in blue) to suit your needs.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim wsDest As Worksheet, wkbSource As Workbook, FolderName As String, bottomB As Long
    Set wsDest = ThisWorkbook.Sheets("Sheet1")
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       FolderName = .SelectedItems(1) & "\"
    End With
    ChDir FolderName
    strExtension = Dir("*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(FolderName & strExtension)
        With Sheets("Sheet1")
            bottomB = .Range("B" & .Rows.Count).End(xlUp).Row
            wsDest.Cells(1, wsDest.Columns.Count).End(xlToLeft).Offset(0, 1).Resize(bottomB).Value = .Range("B1:B" & bottomB).Value
        End With
        wkbSource.Close False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
Thank you for your effort to write the macro code.
 
Upvote 0
Try replacing this line of code:
VBA Code:
Set wsDest = ThisWorkbook.Sheets("Sheet1")
with this line:
VBA Code:
Set wsDest = ThisWorkbook.Sheets(1)
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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