ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Copy range of many files in directory. (https://www.excelbanter.com/excel-programming/271675-copy-range-many-files-directory.html)

CLUPE

Copy range of many files in directory.
 
Hi,

I have a lot of files in a directory (numbers vary over time). I want
to copy the same range in each of the files to a new file.

How do I do that?

pancho[_4_]

Copy range of many files in directory.
 
Use the following code:
Sub readingFiles()
On Error Resume Next
Dim fs As FileSearch
Dim fileStr As String, wb As Workbook, wbNew As
Workbook
Dim r As Long
Set wbNew = Workbooks.Add ' you can use your own
workbook to copy the ranges
Set fs = Application.FileSearch
fs.LookIn = "C:\YourPath" ' You need to set files path
fs.NewSearch
fs.FileType = msoFileTypeExcelWorkbooks
If fs.Execute(SortBy:=msoSortByFileName,
SortOrder:=msoSortOrderAscending) 0 Then
For i = 1 To fs.FoundFiles.Count
Set wb = Workbooks.Open(fs.FoundFiles(i))
' next you can set the sheet and range you need
wb.Sheets("Rep").Range("A1:D20").Copy
' next you can use the sheet you need
r = wbNew.Sheets("Sheet1").Cells.SpecialCells
(xlLastCell).Row
' next you can use the column you want
wbNew.Activate
wbNew.Sheets("Sheet1").Cells(r + 1, 1).Select
Application.DisplayAlerts = False
wbNew.Sheets("Sheet1").Paste
Application.CutCopyMode = False
wb.Close False
Next i
End If
End Sub


Hope this can be useful

Francisco Mariscal

fcomariscal at hotmail dot com

-----Original Message-----
Hi,

I have a lot of files in a directory (numbers vary over

time). I want
to copy the same range in each of the files to a new file.

How do I do that?
.



All times are GMT +1. The time now is 08:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com