LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default How to split and consolidate sheets into new workbooks


This piece of code takes all the spreadsheets in a lower directory
called SOURCE and creates all the new spreadsheets in a Directory that
must exist called target

root - contains this code
root\source - contains source spreadsheets and nothing else
root\target - all new spreadsheets created here
Option Explicit

Sub reMix()
Dim strSourcePath As String
Dim strSource As String
Dim strTarget As String
Dim strFiles(1 To 50) As String
Dim iFiles As Integer
Dim i As Integer

Dim wkbSource As Workbook
Dim wkbTarget As Workbook

Dim wksSource As Worksheet
Dim wksTarget As Worksheet
Dim wks As Worksheet

Dim str As String

strSourcePath = ThisWorkbook.Path & "\source\"
strTarget = ThisWorkbook.Path & "\target\"

str = Dir(strSourcePath & "*.xls")
iFiles = 0
Do Until str = ""
iFiles = iFiles + 1
strFiles(iFiles) = str
str = Dir()
Loop


For i = 1 To iFiles
strSource = strFiles(i)
Set wkbSource = Workbooks.Open(strSourcePath & strSource)
For Each wks In wkbSource.Worksheets()
str = strTarget & wks.Name & ".xls"
If Dir(str) = "" Then
wks.Copy
Set wkbTarget = ActiveWorkbook
Set wksTarget = ActiveSheet
wkbTarget.SaveAs str
Else
Set wkbTarget = Workbooks().Open(str)
wks.Copy wkbTarget.Worksheets(1)
Set wksTarget = ActiveSheet
End If
wksTarget.Name = Left(wkbSource.Name, Len(wkbSource.Name) -
4)
Set wksTarget = Nothing

wkbTarget.Close xlYes
Set wkbTarget = Nothing
Next
wkbSource.Close xlNo
Set wkbSource = Nothing
Next
MsgBox "done"
End Sub


you should have error trapping etc

hope it helps


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=503262

 
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
Consolidate Several Workbooks into One jeannie v Excel Worksheet Functions 1 August 25th 06 12:18 PM
Consolidate multiple workbooks BillMarshall Excel Worksheet Functions 2 November 26th 05 09:40 AM
consolidate 2 different workbooks JackieB Excel Discussion (Misc queries) 1 July 29th 05 12:02 AM
Consolidate different sheets to different workbooks markx Excel Worksheet Functions 3 April 26th 05 03:30 PM
Consolidate data from several workbooks kwatch Excel Worksheet Functions 0 April 14th 05 09:58 PM


All times are GMT +1. The time now is 01:36 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"