PC Review


Reply
Thread Tools Rate Thread

Copy two sheets from workbook & create new workbook

 
 
Mike R.
Guest
Posts: n/a
 
      7th Aug 2009
Hello,
I would like to copy two worksheets (formatting and all...) from one
workbook and create a new workbook with just those two sheets. Here is the
code I used to copy one worksheet, but I can not figure out how to do two. I
am not opposed to all new coding:

' save worksheet with this file name
FilePathName = Sheets("Data").Range("I23") & "\" &
Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
& ".xls"
newsht = ActiveSheet.Copy
Set Wkb = ActiveWorkbook
Wkb.Sheets("Current Openings").Name = "Current Openings"
With ActiveWorkbook
.SaveAs Filename:=FilePathName
.Close False
End With

Thank you in advance for your help!
 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      7th Aug 2009
Hi mike

See how i do it in this mail macro with a array
http://www.rondebruin.nl/mail/folder1/mail3.htm

I also create a temporary window to avoid a bug
It is up to you if you want to use it



--

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




"Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
> Hello,
> I would like to copy two worksheets (formatting and all...) from one
> workbook and create a new workbook with just those two sheets. Here is the
> code I used to copy one worksheet, but I can not figure out how to do two. I
> am not opposed to all new coding:
>
> ' save worksheet with this file name
> FilePathName = Sheets("Data").Range("I23") & "\" &
> Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> & ".xls"
> newsht = ActiveSheet.Copy
> Set Wkb = ActiveWorkbook
> Wkb.Sheets("Current Openings").Name = "Current Openings"
> With ActiveWorkbook
> .SaveAs Filename:=FilePathName
> .Close False
> End With
>
> Thank you in advance for your help!

 
Reply With Quote
 
john
Guest
Posts: n/a
 
      7th Aug 2009
not tested but something like this maybe:

Dim Wkb As Workbook
Dim FilePathName As String
With Sheets("Data")

FilePathName = .Range("I23") & "\" & _
.Range("I19") & "\" & _
.Range("I21") & "\" & _
Date$ & ".xls"

End With

'sheets you want to copy
'- change as required
Sheets(Array("Sheet1", "Sheet2")).Copy

Set Wkb = ActiveWorkbook

With Wkb

.SaveAs Filename:=FilePathName
.Close False

End With
--
jb


"Mike R." wrote:

> Hello,
> I would like to copy two worksheets (formatting and all...) from one
> workbook and create a new workbook with just those two sheets. Here is the
> code I used to copy one worksheet, but I can not figure out how to do two. I
> am not opposed to all new coding:
>
> ' save worksheet with this file name
> FilePathName = Sheets("Data").Range("I23") & "\" &
> Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> & ".xls"
> newsht = ActiveSheet.Copy
> Set Wkb = ActiveWorkbook
> Wkb.Sheets("Current Openings").Name = "Current Openings"
> With ActiveWorkbook
> .SaveAs Filename:=FilePathName
> .Close False
> End With
>
> Thank you in advance for your help!

 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      7th Aug 2009
xl2003

Sub copyshtstonewwb()
mp = ActiveWorkbook.Name
'MsgBox mp
Sheets("Sheet4").Copy
Workbooks(mp).Sheets("sheet5").Copy _
after:=Sheets(Sheets.Count)
ActiveWorkbook.SaveAs Filename:="newfilename"
End Sub



--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Mike R." <(E-Mail Removed)> wrote in message
news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
> Hello,
> I would like to copy two worksheets (formatting and all...) from one
> workbook and create a new workbook with just those two sheets. Here is
> the
> code I used to copy one worksheet, but I can not figure out how to do two.
> I
> am not opposed to all new coding:
>
> ' save worksheet with this file name
> FilePathName = Sheets("Data").Range("I23") & "\" &
> Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" &
> Date$
> & ".xls"
> newsht = ActiveSheet.Copy
> Set Wkb = ActiveWorkbook
> Wkb.Sheets("Current Openings").Name = "Current Openings"
> With ActiveWorkbook
> .SaveAs Filename:=FilePathName
> .Close False
> End With
>
> Thank you in advance for your help!


 
Reply With Quote
 
Mike R.
Guest
Posts: n/a
 
      7th Aug 2009
Hi Ron,
For some reason, I am getting a "Subscript out of range" error on the Array
line of code. I did change the Sheets to my actual sheets I want to copy.

Help...
Mike

"Ron de Bruin" wrote:

> Hi mike
>
> See how i do it in this mail macro with a array
> http://www.rondebruin.nl/mail/folder1/mail3.htm
>
> I also create a temporary window to avoid a bug
> It is up to you if you want to use it
>
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
>
>
> "Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
> > Hello,
> > I would like to copy two worksheets (formatting and all...) from one
> > workbook and create a new workbook with just those two sheets. Here is the
> > code I used to copy one worksheet, but I can not figure out how to do two. I
> > am not opposed to all new coding:
> >
> > ' save worksheet with this file name
> > FilePathName = Sheets("Data").Range("I23") & "\" &
> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> > & ".xls"
> > newsht = ActiveSheet.Copy
> > Set Wkb = ActiveWorkbook
> > Wkb.Sheets("Current Openings").Name = "Current Openings"
> > With ActiveWorkbook
> > .SaveAs Filename:=FilePathName
> > .Close False
> > End With
> >
> > Thank you in advance for your help!

>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      7th Aug 2009
Show me the code Mike that you use now


--

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




"Mike R." <(E-Mail Removed)> wrote in message news:BDFD190D-A2D5-4A95-BC0E-(E-Mail Removed)...
> Hi Ron,
> For some reason, I am getting a "Subscript out of range" error on the Array
> line of code. I did change the Sheets to my actual sheets I want to copy.
>
> Help...
> Mike
>
> "Ron de Bruin" wrote:
>
>> Hi mike
>>
>> See how i do it in this mail macro with a array
>> http://www.rondebruin.nl/mail/folder1/mail3.htm
>>
>> I also create a temporary window to avoid a bug
>> It is up to you if you want to use it
>>
>>
>>
>> --
>>
>> Regards Ron de Bruin
>> http://www.rondebruin.nl/tips.htm
>>
>>
>>
>>
>> "Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
>> > Hello,
>> > I would like to copy two worksheets (formatting and all...) from one
>> > workbook and create a new workbook with just those two sheets. Here is the
>> > code I used to copy one worksheet, but I can not figure out how to do two. I
>> > am not opposed to all new coding:
>> >
>> > ' save worksheet with this file name
>> > FilePathName = Sheets("Data").Range("I23") & "\" &
>> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
>> > & ".xls"
>> > newsht = ActiveSheet.Copy
>> > Set Wkb = ActiveWorkbook
>> > Wkb.Sheets("Current Openings").Name = "Current Openings"
>> > With ActiveWorkbook
>> > .SaveAs Filename:=FilePathName
>> > .Close False
>> > End With
>> >
>> > Thank you in advance for your help!

>>

 
Reply With Quote
 
Mike R.
Guest
Posts: n/a
 
      7th Aug 2009
Here it is... thanks Ron.

Sub aUpdate_Daily()
Dim FilePathName As String

Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim sh As Worksheet
Dim TheActiveWindow As Window
Dim TempWindow As Window

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set Sourcewb = ActiveWorkbook

'Copy the sheets to a new workbook
'We add a temporary Window to avoid the Copy problem
'if there is a List or Table in one of the sheets and
'if the sheets are grouped
With Sourcewb
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Sheet10", "Sheet13")).Copy
End With

'Close temporary Window
TempWindow.Close

Set Destwb = ActiveWorkbook

' 'Change all cells in the worksheets to values if you want
For Each sh In Destwb.Worksheets
sh.Select
With sh.UsedRange
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Destwb.Worksheets(1).Select
Next sh

'Save the new workbook/Mail it/Delete it
FilePathName = Sheets("Data").Range("I23") & "\" &
Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
& ".xls"
With Destwb
.SaveAs FilePathName
.Close SaveChanges:=False
End With

With Application
.ScreenUpdating = True
.EnableEvents = True
End With


End Sub


"Ron de Bruin" wrote:

> Show me the code Mike that you use now
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
>
>
> "Mike R." <(E-Mail Removed)> wrote in message news:BDFD190D-A2D5-4A95-BC0E-(E-Mail Removed)...
> > Hi Ron,
> > For some reason, I am getting a "Subscript out of range" error on the Array
> > line of code. I did change the Sheets to my actual sheets I want to copy.
> >
> > Help...
> > Mike
> >
> > "Ron de Bruin" wrote:
> >
> >> Hi mike
> >>
> >> See how i do it in this mail macro with a array
> >> http://www.rondebruin.nl/mail/folder1/mail3.htm
> >>
> >> I also create a temporary window to avoid a bug
> >> It is up to you if you want to use it
> >>
> >>
> >>
> >> --
> >>
> >> Regards Ron de Bruin
> >> http://www.rondebruin.nl/tips.htm
> >>
> >>
> >>
> >>
> >> "Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
> >> > Hello,
> >> > I would like to copy two worksheets (formatting and all...) from one
> >> > workbook and create a new workbook with just those two sheets. Here is the
> >> > code I used to copy one worksheet, but I can not figure out how to do two. I
> >> > am not opposed to all new coding:
> >> >
> >> > ' save worksheet with this file name
> >> > FilePathName = Sheets("Data").Range("I23") & "\" &
> >> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> >> > & ".xls"
> >> > newsht = ActiveSheet.Copy
> >> > Set Wkb = ActiveWorkbook
> >> > Wkb.Sheets("Current Openings").Name = "Current Openings"
> >> > With ActiveWorkbook
> >> > .SaveAs Filename:=FilePathName
> >> > .Close False
> >> > End With
> >> >
> >> > Thank you in advance for your help!
> >>

>

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      7th Aug 2009
Hi Mike

The sheet names are not correct then
Maybe there is a space before or after the sheet name

Check this out

--

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




"Mike R." <(E-Mail Removed)> wrote in message news:3747B84A-98F1-4217-9C91-(E-Mail Removed)...
> Here it is... thanks Ron.
>
> Sub aUpdate_Daily()
> Dim FilePathName As String
>
> Dim FileExtStr As String
> Dim FileFormatNum As Long
> Dim Sourcewb As Workbook
> Dim Destwb As Workbook
> Dim sh As Worksheet
> Dim TheActiveWindow As Window
> Dim TempWindow As Window
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> Set Sourcewb = ActiveWorkbook
>
> 'Copy the sheets to a new workbook
> 'We add a temporary Window to avoid the Copy problem
> 'if there is a List or Table in one of the sheets and
> 'if the sheets are grouped
> With Sourcewb
> Set TheActiveWindow = ActiveWindow
> Set TempWindow = .NewWindow
> .Sheets(Array("Sheet10", "Sheet13")).Copy
> End With
>
> 'Close temporary Window
> TempWindow.Close
>
> Set Destwb = ActiveWorkbook
>
> ' 'Change all cells in the worksheets to values if you want
> For Each sh In Destwb.Worksheets
> sh.Select
> With sh.UsedRange
> .Cells.Copy
> .Cells.PasteSpecial xlPasteValues
> .Cells(1).Select
> End With
> Application.CutCopyMode = False
> Destwb.Worksheets(1).Select
> Next sh
>
> 'Save the new workbook/Mail it/Delete it
> FilePathName = Sheets("Data").Range("I23") & "\" &
> Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> & ".xls"
> With Destwb
> .SaveAs FilePathName
> .Close SaveChanges:=False
> End With
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
>
>
> End Sub
>
>
> "Ron de Bruin" wrote:
>
>> Show me the code Mike that you use now
>>
>>
>> --
>>
>> Regards Ron de Bruin
>> http://www.rondebruin.nl/tips.htm
>>
>>
>>
>>
>> "Mike R." <(E-Mail Removed)> wrote in message news:BDFD190D-A2D5-4A95-BC0E-(E-Mail Removed)...
>> > Hi Ron,
>> > For some reason, I am getting a "Subscript out of range" error on the Array
>> > line of code. I did change the Sheets to my actual sheets I want to copy.
>> >
>> > Help...
>> > Mike
>> >
>> > "Ron de Bruin" wrote:
>> >
>> >> Hi mike
>> >>
>> >> See how i do it in this mail macro with a array
>> >> http://www.rondebruin.nl/mail/folder1/mail3.htm
>> >>
>> >> I also create a temporary window to avoid a bug
>> >> It is up to you if you want to use it
>> >>
>> >>
>> >>
>> >> --
>> >>
>> >> Regards Ron de Bruin
>> >> http://www.rondebruin.nl/tips.htm
>> >>
>> >>
>> >>
>> >>
>> >> "Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
>> >> > Hello,
>> >> > I would like to copy two worksheets (formatting and all...) from one
>> >> > workbook and create a new workbook with just those two sheets. Here is the
>> >> > code I used to copy one worksheet, but I can not figure out how to do two. I
>> >> > am not opposed to all new coding:
>> >> >
>> >> > ' save worksheet with this file name
>> >> > FilePathName = Sheets("Data").Range("I23") & "\" &
>> >> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
>> >> > & ".xls"
>> >> > newsht = ActiveSheet.Copy
>> >> > Set Wkb = ActiveWorkbook
>> >> > Wkb.Sheets("Current Openings").Name = "Current Openings"
>> >> > With ActiveWorkbook
>> >> > .SaveAs Filename:=FilePathName
>> >> > .Close False
>> >> > End With
>> >> >
>> >> > Thank you in advance for your help!
>> >>

>>

 
Reply With Quote
 
Mike R.
Guest
Posts: n/a
 
      7th Aug 2009
got it... yep, I goofed the sheet names. Thanks Ron!

"Ron de Bruin" wrote:

> Hi Mike
>
> The sheet names are not correct then
> Maybe there is a space before or after the sheet name
>
> Check this out
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
>
>
> "Mike R." <(E-Mail Removed)> wrote in message news:3747B84A-98F1-4217-9C91-(E-Mail Removed)...
> > Here it is... thanks Ron.
> >
> > Sub aUpdate_Daily()
> > Dim FilePathName As String
> >
> > Dim FileExtStr As String
> > Dim FileFormatNum As Long
> > Dim Sourcewb As Workbook
> > Dim Destwb As Workbook
> > Dim sh As Worksheet
> > Dim TheActiveWindow As Window
> > Dim TempWindow As Window
> >
> > With Application
> > .ScreenUpdating = False
> > .EnableEvents = False
> > End With
> >
> > Set Sourcewb = ActiveWorkbook
> >
> > 'Copy the sheets to a new workbook
> > 'We add a temporary Window to avoid the Copy problem
> > 'if there is a List or Table in one of the sheets and
> > 'if the sheets are grouped
> > With Sourcewb
> > Set TheActiveWindow = ActiveWindow
> > Set TempWindow = .NewWindow
> > .Sheets(Array("Sheet10", "Sheet13")).Copy
> > End With
> >
> > 'Close temporary Window
> > TempWindow.Close
> >
> > Set Destwb = ActiveWorkbook
> >
> > ' 'Change all cells in the worksheets to values if you want
> > For Each sh In Destwb.Worksheets
> > sh.Select
> > With sh.UsedRange
> > .Cells.Copy
> > .Cells.PasteSpecial xlPasteValues
> > .Cells(1).Select
> > End With
> > Application.CutCopyMode = False
> > Destwb.Worksheets(1).Select
> > Next sh
> >
> > 'Save the new workbook/Mail it/Delete it
> > FilePathName = Sheets("Data").Range("I23") & "\" &
> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> > & ".xls"
> > With Destwb
> > .SaveAs FilePathName
> > .Close SaveChanges:=False
> > End With
> >
> > With Application
> > .ScreenUpdating = True
> > .EnableEvents = True
> > End With
> >
> >
> > End Sub
> >
> >
> > "Ron de Bruin" wrote:
> >
> >> Show me the code Mike that you use now
> >>
> >>
> >> --
> >>
> >> Regards Ron de Bruin
> >> http://www.rondebruin.nl/tips.htm
> >>
> >>
> >>
> >>
> >> "Mike R." <(E-Mail Removed)> wrote in message news:BDFD190D-A2D5-4A95-BC0E-(E-Mail Removed)...
> >> > Hi Ron,
> >> > For some reason, I am getting a "Subscript out of range" error on the Array
> >> > line of code. I did change the Sheets to my actual sheets I want to copy.
> >> >
> >> > Help...
> >> > Mike
> >> >
> >> > "Ron de Bruin" wrote:
> >> >
> >> >> Hi mike
> >> >>
> >> >> See how i do it in this mail macro with a array
> >> >> http://www.rondebruin.nl/mail/folder1/mail3.htm
> >> >>
> >> >> I also create a temporary window to avoid a bug
> >> >> It is up to you if you want to use it
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >>
> >> >> Regards Ron de Bruin
> >> >> http://www.rondebruin.nl/tips.htm
> >> >>
> >> >>
> >> >>
> >> >>
> >> >> "Mike R." <(E-Mail Removed)> wrote in message news:46C4A27F-BC16-4D42-B441-(E-Mail Removed)...
> >> >> > Hello,
> >> >> > I would like to copy two worksheets (formatting and all...) from one
> >> >> > workbook and create a new workbook with just those two sheets. Here is the
> >> >> > code I used to copy one worksheet, but I can not figure out how to do two. I
> >> >> > am not opposed to all new coding:
> >> >> >
> >> >> > ' save worksheet with this file name
> >> >> > FilePathName = Sheets("Data").Range("I23") & "\" &
> >> >> > Sheets("Data").Range("I19") & "\" & Sheets("Data").Range("I21") & "\" & Date$
> >> >> > & ".xls"
> >> >> > newsht = ActiveSheet.Copy
> >> >> > Set Wkb = ActiveWorkbook
> >> >> > Wkb.Sheets("Current Openings").Name = "Current Openings"
> >> >> > With ActiveWorkbook
> >> >> > .SaveAs Filename:=FilePathName
> >> >> > .Close False
> >> >> > End With
> >> >> >
> >> >> > Thank you in advance for your help!
> >> >>
> >>

>

 
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
Re: Copy all worksheets to another workbook, excl. duplicate sheets already in other workbook Chip Pearson Microsoft Excel Programming 0 26th May 2009 04:27 PM
Create New Workbook and copy sheets from my Speadsheet Jeffrey Microsoft Excel Programming 4 24th Apr 2009 03:42 AM
Create new workbook, temporarily name it and copy sheets to it =?Utf-8?B?RGVyYnlKaW0=?= Microsoft Excel Programming 4 12th Jan 2006 05:03 PM
copy sheets in workbook to new workbook Ctech Microsoft Excel Programming 1 23rd Nov 2005 08:09 PM
create workbook, copy sheets Pierre via OfficeKB.com Microsoft Excel Programming 0 25th Oct 2005 07:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:59 PM.