Copying data from all workbooks in folder into current Worksheet

Grange2

New Member
Joined
Mar 31, 2021
Messages
14
Office Version
  1. 2013
  2. 2011
Platform
  1. Windows
Greetings Everyone,

I'm looking to copy the first 2 columns from all the CSV files in a folder. All csv files have only 1 worksheet and they all have different names.
I'm looking to try and see if its possible to copy and paste the data so it overwrites what was previously copied and pasted with each iteration(new file opened).
The issue i come across is that it doesn't actually paste the new information into the sheet. (I understand that im overwriting the existing data but i plan to call a macro that will do something with the data before the next iteration.)
This is what i have so far:


Sub Copydatafolder()
Dim fName As Variant
Dim ws As Worksheet

Set ws = ActiveWorkbook.Sheets("Sheet1")

fName = Dir("Filedestination\*.csv")

While fName <> ""

Columns("A:B").Select
Selection.Copy

ws.Range("A1").PasteSpecial

fileName = Dir

Wend


I would appreciate your assistance with this :)

Thank you,
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,317
Office Version
  1. 365
Platform
  1. Windows
You need to open the files like
VBA Code:
   Dim fName As String, Pth As String
   Dim ws As Worksheet
   Dim Wbk As Workbook
   
   Set ws = ActiveWorkbook.Sheets("Sheet1")
   Pth = "C:\Mrexcel\"
   fName = Dir(Pth & "*.csv")
   
   Do While fName <> ""
      Set Wbk = Workbooks.Open(Pth & fName)
      Wbk.Sheets(1).Columns("A:B").Copy ws.Range("A1")
      Wbk.Close False
      FileName = Dir
   Loop
Change Pth to your actual file path
 
Solution

Grange2

New Member
Joined
Mar 31, 2021
Messages
14
Office Version
  1. 2013
  2. 2011
Platform
  1. Windows
Thank you!

That makes more sense! That works perfectly.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
60,317
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Forum statistics

Threads
1,137,113
Messages
5,679,704
Members
419,850
Latest member
Cbell07958

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
Top