PC Review


Reply
Thread Tools Rate Thread

Combining several workbooks into one file

 
 
PJ
Guest
Posts: n/a
 
      6th Nov 2008
I have 4 individual workbooks that are generated as part of our
monthly/quarterly reporting and want to automate the process of combining
each workbook into one file with 4 worksheets. Each workbook has the same
data/columns and similar naming convention. The name of the worksheet in
each file is the same as the file name (see below).

Sheet Name Workbook Name
FIN_20080630 FIN_20080630.xls
HRS_20080630 HRS_20080630.xls
GEN_20080630 GEN_20080630.xls
ISS_20080630 ISS_20080630.xls

I found the code below in a post by Tom Ogilvy and was able to modify it for
my needs by hard coding the file names. What I would like to do is create a
template with a button to prompt the user for the cycle date and pull the
corresponding files into a new workbook. I would also like to know if it's
possible to run the TRIM function on a specific column in each of the files
as part of the process.

Sub Combinebooks()
Dim sPath as String
Dim bk1 as Workbook
Dim bk2 as Workbook
Dim bk3 as Workbook
Dim bk4 as Workbook

spath = "C:\Documents and Settings\en14259\Desktop\EOM Reports\"

if dir(sPath & "Consolidated20080630.xls") <> "" then
kill sPath & "Consolidated20080630.xls"
End if

set bk1 = workbooks.open(spath & "FIN_20080630.xls")
set bk2 = workbooks.Open(sPath & "HRS_20080630.xls")
set bk3 = workbooks.Open(sPath & "GEN_20080630.xls")
set bk4 = workbooks.Open(sPath & "ISS_20080630.xls")
bk2.worksheets(1).copy After:=bk1.worksheets(1)
bk1.worksheets(2).name = "HRS_20080630"
bk3.worksheets(1).copy After:=bk1.worksheets(2)
bk1.worksheets(3).name = "GEN_20080630"
bk4.worksheets(1).copy After:=bk1.worksheets(3)
bk1.worksheets(4).name = "ISS_20080630"
bk1.worksheets(1).name = "FIN_20080630"
bk1.SaveAs sPath & "Consolidated20080630.xls"
bk1.close Savechanges:=False
bk2.close Savechanges:=False
bk3.close Savechanges:=False
bk4.close Savechanges:=False
End Sub
 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      6th Nov 2008
Hi PJ

You can try the code in the download of this page
http://www.rondebruin.nl/fso.htm



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"PJ" <(E-Mail Removed)> wrote in message news:94205F4C-C9FC-4081-8CA5-(E-Mail Removed)...
>I have 4 individual workbooks that are generated as part of our
> monthly/quarterly reporting and want to automate the process of combining
> each workbook into one file with 4 worksheets. Each workbook has the same
> data/columns and similar naming convention. The name of the worksheet in
> each file is the same as the file name (see below).
>
> Sheet Name Workbook Name
> FIN_20080630 FIN_20080630.xls
> HRS_20080630 HRS_20080630.xls
> GEN_20080630 GEN_20080630.xls
> ISS_20080630 ISS_20080630.xls
>
> I found the code below in a post by Tom Ogilvy and was able to modify it for
> my needs by hard coding the file names. What I would like to do is create a
> template with a button to prompt the user for the cycle date and pull the
> corresponding files into a new workbook. I would also like to know if it's
> possible to run the TRIM function on a specific column in each of the files
> as part of the process.
>
> Sub Combinebooks()
> Dim sPath as String
> Dim bk1 as Workbook
> Dim bk2 as Workbook
> Dim bk3 as Workbook
> Dim bk4 as Workbook
>
> spath = "C:\Documents and Settings\en14259\Desktop\EOM Reports\"
>
> if dir(sPath & "Consolidated20080630.xls") <> "" then
> kill sPath & "Consolidated20080630.xls"
> End if
>
> set bk1 = workbooks.open(spath & "FIN_20080630.xls")
> set bk2 = workbooks.Open(sPath & "HRS_20080630.xls")
> set bk3 = workbooks.Open(sPath & "GEN_20080630.xls")
> set bk4 = workbooks.Open(sPath & "ISS_20080630.xls")
> bk2.worksheets(1).copy After:=bk1.worksheets(1)
> bk1.worksheets(2).name = "HRS_20080630"
> bk3.worksheets(1).copy After:=bk1.worksheets(2)
> bk1.worksheets(3).name = "GEN_20080630"
> bk4.worksheets(1).copy After:=bk1.worksheets(3)
> bk1.worksheets(4).name = "ISS_20080630"
> bk1.worksheets(1).name = "FIN_20080630"
> bk1.SaveAs sPath & "Consolidated20080630.xls"
> bk1.close Savechanges:=False
> bk2.close Savechanges:=False
> bk3.close Savechanges:=False
> bk4.close Savechanges:=False
> End Sub

 
Reply With Quote
 
PJ
Guest
Posts: n/a
 
      7th Nov 2008
Nice site Ron. I will see if I can get it to work using your example.

Thanks!

"Ron de Bruin" wrote:

> Hi PJ
>
> You can try the code in the download of this page
> http://www.rondebruin.nl/fso.htm
>
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "PJ" <(E-Mail Removed)> wrote in message news:94205F4C-C9FC-4081-8CA5-(E-Mail Removed)...
> >I have 4 individual workbooks that are generated as part of our
> > monthly/quarterly reporting and want to automate the process of combining
> > each workbook into one file with 4 worksheets. Each workbook has the same
> > data/columns and similar naming convention. The name of the worksheet in
> > each file is the same as the file name (see below).
> >
> > Sheet Name Workbook Name
> > FIN_20080630 FIN_20080630.xls
> > HRS_20080630 HRS_20080630.xls
> > GEN_20080630 GEN_20080630.xls
> > ISS_20080630 ISS_20080630.xls
> >
> > I found the code below in a post by Tom Ogilvy and was able to modify it for
> > my needs by hard coding the file names. What I would like to do is create a
> > template with a button to prompt the user for the cycle date and pull the
> > corresponding files into a new workbook. I would also like to know if it's
> > possible to run the TRIM function on a specific column in each of the files
> > as part of the process.
> >
> > Sub Combinebooks()
> > Dim sPath as String
> > Dim bk1 as Workbook
> > Dim bk2 as Workbook
> > Dim bk3 as Workbook
> > Dim bk4 as Workbook
> >
> > spath = "C:\Documents and Settings\en14259\Desktop\EOM Reports\"
> >
> > if dir(sPath & "Consolidated20080630.xls") <> "" then
> > kill sPath & "Consolidated20080630.xls"
> > End if
> >
> > set bk1 = workbooks.open(spath & "FIN_20080630.xls")
> > set bk2 = workbooks.Open(sPath & "HRS_20080630.xls")
> > set bk3 = workbooks.Open(sPath & "GEN_20080630.xls")
> > set bk4 = workbooks.Open(sPath & "ISS_20080630.xls")
> > bk2.worksheets(1).copy After:=bk1.worksheets(1)
> > bk1.worksheets(2).name = "HRS_20080630"
> > bk3.worksheets(1).copy After:=bk1.worksheets(2)
> > bk1.worksheets(3).name = "GEN_20080630"
> > bk4.worksheets(1).copy After:=bk1.worksheets(3)
> > bk1.worksheets(4).name = "ISS_20080630"
> > bk1.worksheets(1).name = "FIN_20080630"
> > bk1.SaveAs sPath & "Consolidated20080630.xls"
> > bk1.close Savechanges:=False
> > bk2.close Savechanges:=False
> > bk3.close Savechanges:=False
> > bk4.close Savechanges:=False
> > End Sub

>

 
Reply With Quote
 
PJ
Guest
Posts: n/a
 
      7th Nov 2008
"Ron de Bruin" wrote:

> Hi PJ
>
> You can try the code in the download of this page
> http://www.rondebruin.nl/fso.htm


Ron, thank you for your assistance. That worked like a charm. I have two
other "minor" inconveniences as part of this process and was hoping you could
offer a suggestion.

I process each of the original files using a data analytics program and
export my results to Excel. The originals in this case are also Excel files
and each contains a column header with characters that causes problems with
the analytics program so I have to manually rename the column before
importing the files. Second, I combine the data from two columns into one
column before exporting back to Excel and the result needs to be trimmed to
remove the extra spaces.

How can I create a similar macro that will search for the "bad" string and
replace it with a new string before I import my files. And also, is it
possible to modify the existing macro to perform a TRIM on each of the
worksheets, which in each case would be everything in column B.

My hope is to develop a master spreadsheet that can be handed off to a
support person who can click a button to run each macro in the process.
These are the last two issues I need.

Thanks in advance.
 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      7th Nov 2008
This will trim all the cells in the used range. If I understand you
correctly, the source file is also an Excel worksheet. The code would need
to be modified to use the source file destination. The filename must be
compatible with the system either showing the file extension or not.
Otherwise you will probably get a subscript out of range message.

Sub celTrim()
Dim c As Range
SourceFile = Workbooks("?.xls") 'Need to sub actual file name
SourceFile.Sheets(1).UsedRange.Select
For Each c In Selection
c = Trim(c.Value)
Next
End Sub

"PJ" wrote:

> "Ron de Bruin" wrote:
>
> > Hi PJ
> >
> > You can try the code in the download of this page
> > http://www.rondebruin.nl/fso.htm

>
> Ron, thank you for your assistance. That worked like a charm. I have two
> other "minor" inconveniences as part of this process and was hoping you could
> offer a suggestion.
>
> I process each of the original files using a data analytics program and
> export my results to Excel. The originals in this case are also Excel files
> and each contains a column header with characters that causes problems with
> the analytics program so I have to manually rename the column before
> importing the files. Second, I combine the data from two columns into one
> column before exporting back to Excel and the result needs to be trimmed to
> remove the extra spaces.
>
> How can I create a similar macro that will search for the "bad" string and
> replace it with a new string before I import my files. And also, is it
> possible to modify the existing macro to perform a TRIM on each of the
> worksheets, which in each case would be everything in column B.
>
> My hope is to develop a master spreadsheet that can be handed off to a
> support person who can click a button to run each macro in the process.
> These are the last two issues I need.
>
> Thanks in advance.

 
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
Combining Workbooks =?Utf-8?B?Um9ubmllRg==?= Microsoft Excel Charting 0 17th May 2007 08:40 PM
combining workbooks Fawn Microsoft Excel Worksheet Functions 0 13th Apr 2005 03:24 AM
Combining Two Workbooks VETcalc Microsoft Excel Discussion 4 4th Sep 2004 03:50 PM
Combining workbooks into one????? ian123 Microsoft Excel Programming 18 17th Dec 2003 06:00 PM
Combining multiple Workbooks into single file Vp Microsoft Excel Worksheet Functions 2 22nd Oct 2003 09:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:40 PM.