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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
 
Upvote 0
Solution
Thank you!

That makes more sense! That works perfectly.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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