PC Review


Reply
Thread Tools Rate Thread

Combine files as tabs in a single Workbook

 
 
Sam Commar
Guest
Posts: n/a
 
      1st Apr 2009
I have 3 Excel files in a folder. I wanted to setup some kind of macro or
programming to combine the three files as three tabs in a single file. Could
someone kindly assist me on this.
2 of the excel files have 2 tabs each and one file has 1 tab. So when they
are combined it makes one file with 5 tabs.


In effect I will have 20 folder each with 3 files -structure will be the
same that is 2 files with 2 tabs and 1 file with one tab and they are to be
made into one file with tabs.



Thanks so much for your help

Sam Commar

 
Reply With Quote
 
 
 
 
joel
Guest
Posts: n/a
 
      1st Apr 2009
The macro below will seach each folder in the Root directroy and combine all
sheets in all workbook into a single workbook. then it will save the new
book in the same directroy using the parent folders name.


Sub Combinebooks()

Root = "c:\Temp"


Set fso = CreateObject _
("Scripting.FileSystemObject")

Set folder = _
fso.GetFolder(Root)

For Each sf In folder.subfolders
First = True
FName = Dir(sf & "\*.xls")
Do While FName <> ""
Set bk = Workbooks.Open(Filename:=sf & "\" & FName)
For Each sht In bk.Sheets
If First = True Then
sht.Copy
Set newbk = ActiveWorkbook
First = False
Else
With newbk
sht.Copy _
after:=.Sheets(.Sheets.Count)
End With
End If
Next sht
bk.Close savechanges:=False
FName = Dir()
Loop
newbk.SaveAs Filename:=sf & "\" & _
sf.Name & ".xls"
newbk.Close
Next sf

End Sub



"Sam Commar" wrote:

> I have 3 Excel files in a folder. I wanted to setup some kind of macro or
> programming to combine the three files as three tabs in a single file. Could
> someone kindly assist me on this.
> 2 of the excel files have 2 tabs each and one file has 1 tab. So when they
> are combined it makes one file with 5 tabs.
>
>
> In effect I will have 20 folder each with 3 files -structure will be the
> same that is 2 files with 2 tabs and 1 file with one tab and they are to be
> made into one file with tabs.
>
>
>
> Thanks so much for your help
>
> Sam Commar
>

 
Reply With Quote
 
Sam Commar
Guest
Posts: n/a
 
      1st Apr 2009
Joel

Thanks so much . I really appreciate it. Ill test it right now.

"joel" <(E-Mail Removed)> wrote in message
newsBAB1892-6B1F-4E10-8A83-(E-Mail Removed)...
> The macro below will seach each folder in the Root directroy and combine
> all
> sheets in all workbook into a single workbook. then it will save the new
> book in the same directroy using the parent folders name.
>
>
> Sub Combinebooks()
>
> Root = "c:\Temp"
>
>
> Set fso = CreateObject _
> ("Scripting.FileSystemObject")
>
> Set folder = _
> fso.GetFolder(Root)
>
> For Each sf In folder.subfolders
> First = True
> FName = Dir(sf & "\*.xls")
> Do While FName <> ""
> Set bk = Workbooks.Open(Filename:=sf & "\" & FName)
> For Each sht In bk.Sheets
> If First = True Then
> sht.Copy
> Set newbk = ActiveWorkbook
> First = False
> Else
> With newbk
> sht.Copy _
> after:=.Sheets(.Sheets.Count)
> End With
> End If
> Next sht
> bk.Close savechanges:=False
> FName = Dir()
> Loop
> newbk.SaveAs Filename:=sf & "\" & _
> sf.Name & ".xls"
> newbk.Close
> Next sf
>
> End Sub
>
>
>
> "Sam Commar" wrote:
>
>> I have 3 Excel files in a folder. I wanted to setup some kind of macro or
>> programming to combine the three files as three tabs in a single file.
>> Could
>> someone kindly assist me on this.
>> 2 of the excel files have 2 tabs each and one file has 1 tab. So when
>> they
>> are combined it makes one file with 5 tabs.
>>
>>
>> In effect I will have 20 folder each with 3 files -structure will be the
>> same that is 2 files with 2 tabs and 1 file with one tab and they are to
>> be
>> made into one file with tabs.
>>
>>
>>
>> Thanks so much for your help
>>
>> Sam Commar
>>

 
Reply With Quote
 
Sam Commar
Guest
Posts: n/a
 
      1st Apr 2009
Joel

When I run it it errors out on the item below

newbk.SaveAs Filename:=sf & "\" & _
sf.Name & ".xls"

"Sam Commar" <(E-Mail Removed)> wrote in message
news:33A905BE-8C52-45D5-9F97-(E-Mail Removed)...
> Joel
>
> Thanks so much . I really appreciate it. Ill test it right now.
>
> "joel" <(E-Mail Removed)> wrote in message
> newsBAB1892-6B1F-4E10-8A83-(E-Mail Removed)...
>> The macro below will seach each folder in the Root directroy and combine
>> all
>> sheets in all workbook into a single workbook. then it will save the new
>> book in the same directroy using the parent folders name.
>>
>>
>> Sub Combinebooks()
>>
>> Root = "c:\Temp"
>>
>>
>> Set fso = CreateObject _
>> ("Scripting.FileSystemObject")
>>
>> Set folder = _
>> fso.GetFolder(Root)
>>
>> For Each sf In folder.subfolders
>> First = True
>> FName = Dir(sf & "\*.xls")
>> Do While FName <> ""
>> Set bk = Workbooks.Open(Filename:=sf & "\" & FName)
>> For Each sht In bk.Sheets
>> If First = True Then
>> sht.Copy
>> Set newbk = ActiveWorkbook
>> First = False
>> Else
>> With newbk
>> sht.Copy _
>> after:=.Sheets(.Sheets.Count)
>> End With
>> End If
>> Next sht
>> bk.Close savechanges:=False
>> FName = Dir()
>> Loop
>> newbk.SaveAs Filename:=sf & "\" & _
>> sf.Name & ".xls"
>> newbk.Close
>> Next sf
>>
>> End Sub
>>
>>
>>
>> "Sam Commar" wrote:
>>
>>> I have 3 Excel files in a folder. I wanted to setup some kind of macro
>>> or
>>> programming to combine the three files as three tabs in a single file.
>>> Could
>>> someone kindly assist me on this.
>>> 2 of the excel files have 2 tabs each and one file has 1 tab. So when
>>> they
>>> are combined it makes one file with 5 tabs.
>>>
>>>
>>> In effect I will have 20 folder each with 3 files -structure will be the
>>> same that is 2 files with 2 tabs and 1 file with one tab and they are to
>>> be
>>> made into one file with tabs.
>>>
>>>
>>>
>>> Thanks so much for your help
>>>
>>> Sam Commar
>>>

 
Reply With Quote
 
Sam Commar
Guest
Posts: n/a
 
      1st Apr 2009
Sorry the error it gives is

Run time error '424'
Object required

"Sam Commar" <(E-Mail Removed)> wrote in message
news:E9AE9A48-05C6-4964-9319-(E-Mail Removed)...
> Joel
>
> When I run it it errors out on the item below
>
> newbk.SaveAs Filename:=sf & "\" & _
> sf.Name & ".xls"
>
> "Sam Commar" <(E-Mail Removed)> wrote in message
> news:33A905BE-8C52-45D5-9F97-(E-Mail Removed)...
>> Joel
>>
>> Thanks so much . I really appreciate it. Ill test it right now.
>>
>> "joel" <(E-Mail Removed)> wrote in message
>> newsBAB1892-6B1F-4E10-8A83-(E-Mail Removed)...
>>> The macro below will seach each folder in the Root directroy and combine
>>> all
>>> sheets in all workbook into a single workbook. then it will save the
>>> new
>>> book in the same directroy using the parent folders name.
>>>
>>>
>>> Sub Combinebooks()
>>>
>>> Root = "c:\Temp"
>>>
>>>
>>> Set fso = CreateObject _
>>> ("Scripting.FileSystemObject")
>>>
>>> Set folder = _
>>> fso.GetFolder(Root)
>>>
>>> For Each sf In folder.subfolders
>>> First = True
>>> FName = Dir(sf & "\*.xls")
>>> Do While FName <> ""
>>> Set bk = Workbooks.Open(Filename:=sf & "\" & FName)
>>> For Each sht In bk.Sheets
>>> If First = True Then
>>> sht.Copy
>>> Set newbk = ActiveWorkbook
>>> First = False
>>> Else
>>> With newbk
>>> sht.Copy _
>>> after:=.Sheets(.Sheets.Count)
>>> End With
>>> End If
>>> Next sht
>>> bk.Close savechanges:=False
>>> FName = Dir()
>>> Loop
>>> newbk.SaveAs Filename:=sf & "\" & _
>>> sf.Name & ".xls"
>>> newbk.Close
>>> Next sf
>>>
>>> End Sub
>>>
>>>
>>>
>>> "Sam Commar" wrote:
>>>
>>>> I have 3 Excel files in a folder. I wanted to setup some kind of macro
>>>> or
>>>> programming to combine the three files as three tabs in a single file.
>>>> Could
>>>> someone kindly assist me on this.
>>>> 2 of the excel files have 2 tabs each and one file has 1 tab. So when
>>>> they
>>>> are combined it makes one file with 5 tabs.
>>>>
>>>>
>>>> In effect I will have 20 folder each with 3 files -structure will be
>>>> the
>>>> same that is 2 files with 2 tabs and 1 file with one tab and they are
>>>> to be
>>>> made into one file with tabs.
>>>>
>>>>
>>>>
>>>> Thanks so much for your help
>>>>
>>>> Sam Commar
>>>>

 
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 multiple workbook then combine into single workbook butdifferent sheets geniusideas Microsoft Excel Programming 3 25th Nov 2009 11:47 PM
How to set up single Header/Footer for all tabs in Excel Workbook. =?Utf-8?B?aGVhZGVycyAmIGZvb3RlcnMgcmVwZWF0ZWQ=?= Microsoft Excel Worksheet Functions 2 17th Jul 2009 12:18 AM
how can I combine multiple worksheets into a single workbook? don_15D Microsoft Excel Misc 5 3rd Jan 2009 07:51 AM
Trying to write macro to combine data in multiple tabs into a single tab. musicheck@gmail.com Microsoft Excel Programming 3 15th Jun 2007 04:16 PM
Re: Linking single sheets to tabs in another workbook Harlan Grove Microsoft Excel Worksheet Functions 0 20th Aug 2004 04:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:26 PM.