Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default problem copy/pasting pages.

Can someone give me code to copy a worksheet from one workbook to a new
workbook? I need to copy 2 sheets. When I try to copy one
(Application..Worksheets("Data1").Copy) it seems to automatically
create a new workbook called "Book1". When I try to copy the second
sheet it creates another workbook. I need to paste the second sheet
into book1 also.

Thanks,
rut

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default problem copy/pasting pages.

Hi rut,

Can someone give me code to copy a worksheet from one workbook to a new
workbook? I need to copy 2 sheets. When I try to copy one
(Application..Worksheets("Data1").Copy) it seems to automatically
create a new workbook called "Book1". When I try to copy the second
sheet it creates another workbook. I need to paste the second sheet
into book1 also.


try something like this:

Sub test()
Workbooks("Test.xls").Worksheets("Data1").Copy _
Befo=Workbooks("Book1.xls").Worksheets(1)
Workbooks("Test.xls").Worksheets("Data2").Copy _
Befo=Workbooks("Book1.xls").Worksheets(1)
End Sub

--
Greetings,
Monika Weber [Microsoft MVP für Excel]
http://www.jumper.ch (Office Help Desk)


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default problem copy/pasting pages.

So if I want to create a new workbook for the first sheet (don't use
Before) but I don't want to for the second sheet, how would that code
look? I'm trying to get away from referencing the new workbook by name
(ie, "book1") as I don't know that the user won't have Excel open and
another workbook already called book1.
I need to be able to use activeworkbook references.

Rut

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.

Another way

worksheets(Array("Data1","Data2")).Copy

--
Regards,
Tom Ogilvy


"Monika Weber" wrote in message
...
Hi rut,

Can someone give me code to copy a worksheet from one workbook to a new
workbook? I need to copy 2 sheets. When I try to copy one
(Application..Worksheets("Data1").Copy) it seems to automatically
create a new workbook called "Book1". When I try to copy the second
sheet it creates another workbook. I need to paste the second sheet
into book1 also.


try something like this:

Sub test()
Workbooks("Test.xls").Worksheets("Data1").Copy _
Befo=Workbooks("Book1.xls").Worksheets(1)
Workbooks("Test.xls").Worksheets("Data2").Copy _
Befo=Workbooks("Book1.xls").Worksheets(1)
End Sub

--
Greetings,
Monika Weber [Microsoft MVP für Excel]
http://www.jumper.ch (Office Help Desk)




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.

Activeworkbook.worksheets(Array("Sheet1","Sheet2") ).copy

creates a new workbook containing a copy of sheet1 and sheet2

The copy is now the active workbook.

Activeworkbook.SaveAs "MyNewBook.xls"

for example.

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
So if I want to create a new workbook for the first sheet (don't use
Before) but I don't want to for the second sheet, how would that code
look? I'm trying to get away from referencing the new workbook by name
(ie, "book1") as I don't know that the user won't have Excel open and
another workbook already called book1.
I need to be able to use activeworkbook references.

Rut





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default problem copy/pasting pages.

Also, the sheets don't appear right after each other in the existing
book, ie there are other sheets between "Data1" and "T-data"

Rut

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.


Public Sub Test1()


Dim wkbk As Workbook
Dim wkbk1 As Workbook
Dim sh As Worksheet
Dim sh1 As Worksheet
Set wkbk = ActiveWorkbook
'Set wkbk1 = Workbooks.Open("C:\MyFolder\MyNewBook.xls")
Set wkbk1 = Workbooks("Book3")
For Each sh In wkbk1.Worksheets
Set sh1 = Nothing
On Error Resume Next
Set sh1 = Worksheets(sh.Name)
On Error GoTo 0
If Not sh1 Is Nothing Then
sh1.Name = sh1.Name & "_delete"
End If
Next
wkbk1.Worksheets.Copy After:= _
wkbk.Worksheets(wkbk.Worksheets.Count)
For Each sh In wkbk.Worksheets
If Right(sh.Name, 7) = "_delete" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next
End Sub

--
Regards,
Tom Ogilvy



wrote in message
oups.com...
OK, that works. Let me ask one more related question. If I want to open
this new book up and copy both sheets back into another workbook that
already has these same sheets (I want to replace existing sheets), how
would that look?

Thanks,
Rut



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.

If you want the sheets to be in the same order, then my code doesn't do
that.

--
Regards,
Tom Ogilvy


wrote in message
oups.com...
Also, the sheets don't appear right after each other in the existing
book, ie there are other sheets between "Data1" and "T-data"

Rut



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.

This should do what you want.

Public Sub Test22()


Dim wkbk As Workbook
Dim wkbk1 As Workbook
Dim sh As Worksheet, sName As String
Dim sh1 As Worksheet, sh2 As Worksheet
Set wkbk = ActiveWorkbook
'Set wkbk1 = Workbooks.Open("C:\MyFolder\MyNewBook.xls")
Set wkbk1 = Workbooks("Book3")
For Each sh In wkbk1.Worksheets
Set sh1 = Nothing
On Error Resume Next
Set sh1 = wkbk.Worksheets(sh.Name)
On Error GoTo 0
If Not sh1 Is Nothing Then
sName = sh.Name
sh.Copy After:=sh1
Set sh2 = ActiveSheet
Application.DisplayAlerts = False
sh1.Delete
Application.DisplayAlerts = True
sh2.Name = sName
Else
sh.Copy After:=wkbk.Worksheets( _
wkbk.Worksheets.Count)
End If
Next

End Sub


--
Regards,
Tom Ogilvy

wrote in message
oups.com...
Also, the sheets don't appear right after each other in the existing
book, ie there are other sheets between "Data1" and "T-data"

Rut



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default problem copy/pasting pages.

Even though this doesn't do what you want,
There was a missing qualifier, so for completeness:

Public Sub Test1()


Dim wkbk As Workbook
Dim wkbk1 As Workbook
Dim sh As Worksheet
Dim sh1 As Worksheet
Set wkbk = ActiveWorkbook
Set wkbk1 = Workbooks.Open("C:\MyFolder\MyNewBook.xls")
For Each sh In wkbk1.Worksheets
Set sh1 = Nothing
On Error Resume Next
Set sh1 = wkbk.Worksheets(sh.Name)
On Error GoTo 0
If Not sh1 Is Nothing Then
sh1.Name = sh1.Name & "_delete"
End If
Next
wkbk1.Worksheets.Copy After:= _
wkbk.Worksheets(wkbk.Worksheets.Count)
For Each sh In wkbk.Worksheets
If Right(sh.Name, 7) = "_delete" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next
End Sub

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...

Public Sub Test1()


Dim wkbk As Workbook
Dim wkbk1 As Workbook
Dim sh As Worksheet
Dim sh1 As Worksheet
Set wkbk = ActiveWorkbook
'Set wkbk1 = Workbooks.Open("C:\MyFolder\MyNewBook.xls")
Set wkbk1 = Workbooks("Book3")
For Each sh In wkbk1.Worksheets
Set sh1 = Nothing
On Error Resume Next
Set sh1 = Worksheets(sh.Name)
On Error GoTo 0
If Not sh1 Is Nothing Then
sh1.Name = sh1.Name & "_delete"
End If
Next
wkbk1.Worksheets.Copy After:= _
wkbk.Worksheets(wkbk.Worksheets.Count)
For Each sh In wkbk.Worksheets
If Right(sh.Name, 7) = "_delete" Then
Application.DisplayAlerts = False
sh.Delete
Application.DisplayAlerts = True
End If
Next
End Sub

--
Regards,
Tom Ogilvy



wrote in message
oups.com...
OK, that works. Let me ask one more related question. If I want to open
this new book up and copy both sheets back into another workbook that
already has these same sheets (I want to replace existing sheets), how
would that look?

Thanks,
Rut





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
Pasting PDF pages into Worksheets Jim Johnson Excel Discussion (Misc queries) 1 November 10th 06 08:50 PM
Copy and Pasting JZ Excel Discussion (Misc queries) 1 July 25th 06 04:31 AM
Copying and Pasting from Web Pages Christina Excel Discussion (Misc queries) 0 March 29th 06 07:56 PM
Copy and Pasting Every Third Row teo2099 Excel Worksheet Functions 0 August 25th 05 07:02 PM
Copy pasting problem neil Excel Discussion (Misc queries) 3 July 13th 05 01:40 PM


All times are GMT +1. The time now is 08:12 AM.

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

About Us

"It's about Microsoft Excel"