Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Copy worksheet from multiple files in one DIR to another DIR & rename

Can anyone share idea(s) for code that will programatically loop
through all the .xls files in a directory and copy the second sheet
all the .xls files in the "C:\My Documents\Data\month01" directory to
a workbook named "tot01.xls" [path is "C:\My
Documents\Data\Consol\tot01.xls"] and then name each sheet copied
using the value in cell C2 of each of the sheets after it is copied?
Any ideas are greatly appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy worksheet from multiple files in one DIR to another DIR & rename

Sub copysheets()
Dim wkbk As Workbook
Dim wkbk1 As Workbook
Dim sName As String
Dim i As Long

On Error Resume Next
Set wkbk = Workbooks("tot01.xls")
On Error GoTo 0
If wkbk Is Nothing Then
Set wkbk = Workbooks.Open( _
"C:\My Documents\Data\Consol\tot01.xls")
End If
With Application.FileSearch
.NewSearch
.LookIn = "C:\My Documents\Data\month01"
.SearchSubFolders = True
.FileName = ".xls"
.MatchTextExactly = True
.FileType = msoFileTypeExcelWorkbooks

If .Execute() 0 Then
For i = 1 To .FoundFiles.Count
Set wkbk1 = Workbooks.Open( _
.FoundFiles(i))
sName = wkbk1.Worksheets(2). _
Range("C2").Value
wkbk1.Worksheets(2).Copy _
After:=wkbk.Worksheets( _
wkbk.Worksheets.Count)
wkbk.Worksheets(wkbk.Worksheets. _
Count).Name = sName
wkbk1.Close SaveChanges:=False
Next i
wkbk.Save
Else
MsgBox "There were no files found."
End If
End With
End Sub


--
Regards,
Tom Ogilvy

Mike Taylor wrote in message
m...
Can anyone share idea(s) for code that will programatically loop
through all the .xls files in a directory and copy the second sheet
all the .xls files in the "C:\My Documents\Data\month01" directory to
a workbook named "tot01.xls" [path is "C:\My
Documents\Data\Consol\tot01.xls"] and then name each sheet copied
using the value in cell C2 of each of the sheets after it is copied?
Any ideas are greatly appreciated.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro: Copy a worksheet and rename it to a specified value mitch Excel Discussion (Misc queries) 2 February 16th 09 05:43 PM
Copy worksheet and rename sheet Ernst - EXE Graphics Excel Discussion (Misc queries) 4 July 25th 08 01:18 PM
Rename multiple csv files eliminating (-) hyphens from the label. simplymidori[_2_] Excel Discussion (Misc queries) 2 July 26th 07 11:34 PM
How to create a copy of a folder having five files in it, & rename Matthews Excel Worksheet Functions 1 November 7th 06 03:42 PM
How do I edit a worksheet and copy to multiple files? artex Excel Discussion (Misc queries) 0 August 31st 05 11:08 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"