PC Review


Reply
Thread Tools Rate Thread

Copy data from all the file file from a folder.

 
 
Heera
Guest
Posts: n/a
 
      18th Aug 2008
Hi,

If suppose I have 10 different files in a folder I want the macro to
open the files one after one and copy the data from the file and paste
it in a different workbook.

While doing the same the macro should also rename the tab with the
files name from which the data is copied.

The number of files may increase or decrease.

I just want to mention the path of the folder I don’t want to copy the
names of the files because every time the file names changes.

Any solution for it.

Regards

Heera
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      18th Aug 2008
I don't know how many sheets from each workbook you need to copy so I'm only
copying the active sheet in each of the opened workbooks. Change "Folder ="
as required.

Sub GetBooks()

Folder = "C:\Temp\"
FName = Dir(Folder & "*.xls")
Do While FName <> ""
Set OldBk = Workbooks.Open(Filename:=Folder & FName)
With ThisWorkbook
OldBk.ActiveSheet.Copy _
after:=.Sheets(.Sheets.Count)
ActiveSheet.Name = FName
End With
OldBk.Close savechanges:=False
FName = Dir()
Loop

End Sub

 
Reply With Quote
 
Heera
Guest
Posts: n/a
 
      18th Aug 2008
Hi,

Joel your code is working fine. I would appriciate if you can explain
me what this macro is doing at each step. It will be a great help for
me next time when ever i write the macro.

Regards

Heera
 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      18th Aug 2008



Sub GetBooks()

Folder = "C:\Temp\"
'Get each XLS file in the Folder, will return an empty string after last file
FName = Dir(Folder & "*.xls")
'loop until there are no more files to open
Do While FName <> ""
'Open sleected file
Set OldBk = Workbooks.Open(Filename:=Folder & FName)
'open workbook become active so must specify original book
With ThisWorkbook
'copy sheet from open book to this book
'Require AFTER otherwise sheet goes in a new workbook
'Place Copied sheet as last sheet in workbook
OldBk.ActiveSheet.Copy _
after:=.Sheets(.Sheets.Count)
'Make sheet name the same as workbook name
ActiveSheet.Name = FName
End With
'close workbook that was opened.
'specify savechanges false so nothing is changed in opened book
'also savechanges are needed so pop-up window doesn't
'show up after every file.
OldBk.Close savechanges:=False
'get next file in search criteria
FName = Dir()
Loop

End Sub


"Heera" wrote:

> Hi,
>
> Joel your code is working fine. I would appriciate if you can explain
> me what this macro is doing at each step. It will be a great help for
> me next time when ever i write the macro.
>
> Regards
>
> Heera
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
open excel file and copy data to another file Norvascom Microsoft Excel Programming 4 7th Jan 2010 10:55 PM
copy data for match word from one file to another file mishak Microsoft Excel Worksheet Functions 1 2nd Dec 2009 02:11 AM
Copy data from new file in a folder K Microsoft Excel Programming 1 22nd Nov 2008 08:03 PM
Help: How to copy items into an Outlook2003 Personal Folder File, from an Outlook2002 PST FIle? Mayo Microsoft Outlook Installation 4 30th Apr 2004 09:37 AM
Help: How to copy items into an Outlook2003 Personal Folder File, from an Outlook2002 PST FIle? Mayo Microsoft Outlook Calendar 2 25th Apr 2004 10:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:03 AM.