Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to copy sheets from several files into a new workbook.

Please help. Is too difficult for my Excel knowledge.

I have several excel files that have the same number of sheets (same
name same sequence).
I need to write a macro that loops through them , takes a first sheet
from each of the excel files and copy them into a new file, then takes
a second sheet from each file and does the same.

For example lets say I have File1.xls, File2.xls,File3.xls.
Each of them consists of Sheet1, Sheet2, Sheet3, Sheet4 and Sheet5.
After this macro runs it should create 5 files - Sheet1.xls,
Sheet2.xls, Sheet3.xls, Sheet4.xls, Sheet5.xls each of them containing
3 sheets - one from each of the original files.

Thank you!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Macro to copy sheets from several files into a new workbook.

Below is some sample code that does something similar to what you are trying
to do. As I do not have any information regarding the names of the
worksheets, I did not attempt to rename the worksheets in the new workbook
after they are copied.


Function CreateWorkbooks()
Dim sFilename As String
Dim wkbTarget As Workbook
Dim wkbDestination As Workbook
Dim iNumOfSheets As Integer
Dim iSheetsInNewWorkbook
Dim i As Integer
Const FILE_DIR = "H:\Test1\"

'Grab the value of the SheetsInNewWorkbook property
iSheetsInNewWorkbook = Application.SheetsInNewWorkbook
'Temporarily reset the property so only one sheet is created in new
workbooks
Application.SheetsInNewWorkbook = 1

'Get the number of worksheets from one of the workbooks, assuming all
have the same number
sFilename = Dir(FILE_DIR & "*.xls")
Set wkbTarget = Workbooks.Open(FILE_DIR & sFilename)
iNumOfSheets = wkbTarget.Worksheets.Count
wkbTarget.Close

'Now we know the sheet count, loop and create the workbooks
For i = 1 To iNumOfSheets
'Create a new workbook to hold the copied sheets
Set wkbDestination = Workbooks.Add
'Get the first .xls file
sFilename = Dir(FILE_DIR & "*.xls")
'The sfilename variable will = "" when all .xls files in the folder
have been iterated
Do While sFilename < ""
'Open the source workbook to copy the sheet, for first sheet
file is already open
Set wkbTarget = Workbooks.Open(FILE_DIR & sFilename)
'Copy the source worksheet to the new workbook, assume sheet
name is SheetX
wkbTarget.Worksheets("Sheet" & CStr(i)).Copy
wkbDestination.Worksheets("Sheet1")
'Close the source workbook without saving
wkbTarget.Close False
'Get the next filename
sFilename = Dir
Loop
'Remove default worksheet Sheet1
Application.DisplayAlerts = False
wkbDestination.Worksheets("Sheet1").Delete
Application.DisplayAlerts = True
'Save the workbook in a subdirectory "NewBooks"
wkbDestination.SaveAs FILE_DIR & "NewBooks\Sheet" & CStr(i) & ".xls"
'Close the workbook
wkbDestination.Close False
Next i

'Reset the SheetInNewWorkbook value back to its previous value
Application.SheetsInNewWorkbook = iSheetsInNewWorkbook

Set wkbTarget = Nothing
Set wkbDestination = Nothing


End Function


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


wrote in message
oups.com...
Please help. Is too difficult for my Excel knowledge.

I have several excel files that have the same number of sheets (same
name same sequence).
I need to write a macro that loops through them , takes a first sheet
from each of the excel files and copy them into a new file, then takes
a second sheet from each file and does the same.

For example lets say I have File1.xls, File2.xls,File3.xls.
Each of them consists of Sheet1, Sheet2, Sheet3, Sheet4 and Sheet5.
After this macro runs it should create 5 files - Sheet1.xls,
Sheet2.xls, Sheet3.xls, Sheet4.xls, Sheet5.xls each of them containing
3 sheets - one from each of the original files.

Thank you!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Macro to copy sheets from several files into a new workbook.

David,

Thank you soooo much -
with minimal changes your code accomplishes exactly what needs to be
done.

You helped me out a lot.
Thanks!

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 to Copy Sheets to new workbook KennyD Excel Discussion (Misc queries) 18 January 31st 10 01:25 PM
workbook sheets into separate files markx Excel Worksheet Functions 1 June 28th 05 04:02 PM
How do I save sheets in a workbook to separate files? Omzala Excel Worksheet Functions 2 January 13th 05 06:23 PM
Import several txt files into separate sheets within 1 workbook Steve[_56_] Excel Programming 0 January 15th 04 10:31 PM
Copy text files into multiple sheets of 1 workbook Steve[_56_] Excel Programming 0 January 14th 04 08:30 PM


All times are GMT +1. The time now is 06:58 AM.

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

About Us

"It's about Microsoft Excel"