Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Copy Worksheet to another workbook, programatically?

Hi Everyone,
What I need to do is programatically copy a worksheet from a workbook (call it
Book1) to another (call it Book2), renaming the Worksheet before it goes into
Book2. The initial condition being that Book2 is closed. I thought I had a way
but I keep running into various problems. I'm not reprinting that here because I
think I need to start fresh. My question is, what is the cleanest way to do it?
Thank You all so much in advance,
-plh

I keep hitting "Esc", but I'm still here!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy Worksheet to another workbook, programatically?

Given Bob's valuable information:

' assume book1.xls is open, book2.xls may be open or not
' assume not default books, but books with these names,
' previously saved

Sub Tester1()
Dim bOpen
Dim bk1 As Workbook
Dim bk2 As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Application.ScreenUpdating = False
Set bk1 = Workbooks("Book1.xls")
Set sh1 = bk1.Worksheets("Sheet1")
On Error Resume Next
Set bk2 = Workbooks("Book2.xls")
bOpen = True
On Error GoTo 0
If bk2 Is Nothing Then
Set bk2 = Workbooks.Open("C:\My Folder\Book2.xls")
End If
sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)
Set sh2 = bk2.Worksheets(bk2.Worksheets.Count)
' or set sh2 = Activesheet
sh2.Name = "ABCD"
If Not bOpen Then
bk2.Close SaveChanges:=True
Else
bk2.Save
End If
Application.ScreenUpdating = True
End Sub

Code untested and may contain typos.
--
Regards,
Tom Ogilvy


Bob Phillips wrote in message
...
You can't copy a worksheet to a closed book, so you will need to open

Book2
first.

--

HTH

Bob Phillips

"plh" wrote in message
...
Hi Everyone,
What I need to do is programatically copy a worksheet from a workbook

(call it
Book1) to another (call it Book2), renaming the Worksheet before it goes

into
Book2. The initial condition being that Book2 is closed. I thought I had

a
way
but I keep running into various problems. I'm not reprinting that here

because I
think I need to start fresh. My question is, what is the cleanest way to

do it?
Thank You all so much in advance,
-plh

I keep hitting "Esc", but I'm still here!





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Copy Worksheet to another workbook, programatically?

Mr Ogilvy,

From his post he seemed relatively able, just wanted to know how to do
something that was not possible. I sought to give him enough information to
stop him wasting his time on that pursuit.

--

HTH

Bob Phillips

"Tom Ogilvy" wrote in message
...
Given Bob's valuable information:

' assume book1.xls is open, book2.xls may be open or not
' assume not default books, but books with these names,
' previously saved

Sub Tester1()
Dim bOpen
Dim bk1 As Workbook
Dim bk2 As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Application.ScreenUpdating = False
Set bk1 = Workbooks("Book1.xls")
Set sh1 = bk1.Worksheets("Sheet1")
On Error Resume Next
Set bk2 = Workbooks("Book2.xls")
bOpen = True
On Error GoTo 0
If bk2 Is Nothing Then
Set bk2 = Workbooks.Open("C:\My Folder\Book2.xls")
End If
sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)
Set sh2 = bk2.Worksheets(bk2.Worksheets.Count)
' or set sh2 = Activesheet
sh2.Name = "ABCD"
If Not bOpen Then
bk2.Close SaveChanges:=True
Else
bk2.Save
End If
Application.ScreenUpdating = True
End Sub

Code untested and may contain typos.
--
Regards,
Tom Ogilvy


Bob Phillips wrote in message
...
You can't copy a worksheet to a closed book, so you will need to open

Book2
first.

--

HTH

Bob Phillips

"plh" wrote in message
...
Hi Everyone,
What I need to do is programatically copy a worksheet from a workbook

(call it
Book1) to another (call it Book2), renaming the Worksheet before it

goes
into
Book2. The initial condition being that Book2 is closed. I thought I

had
a
way
but I keep running into various problems. I'm not reprinting that here

because I
think I need to start fresh. My question is, what is the cleanest way

to
do it?
Thank You all so much in advance,
-plh

I keep hitting "Esc", but I'm still here!







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy Worksheet to another workbook, programatically?

Apparently you misinterpreted my statement and are trying to turn it into
sometype of affront.

I said
"Given Bob's valuable information: "
and you seem to interpret it as some kind of sarcasm - which was not
intended. I suspect 99.9% of people reading that would take no offense.
While it isn't worth the search, if you did, you would find I often use such
terminology to acknowledge someone else's post to indicate I have read it
and agree. This is the first time someone has turned it into an insult.

Why not get a positive outlook on life and quit badgering people with
pettiness. The purpose of the group is technical discussion and assistance.

the OP clearly stated:

I thought I had a way
but I keep running into various problems. I'm not reprinting that here

because I
think I need to start fresh. My question is, what is the cleanest way to do

it?

so given your response (which I acknowledged), I provided one approach,
illustrating your suggestion, to work around the problem (which would appear
to the user that book2.xls had not been opened). If you felt the OP didn't
want sample code, your welcome to your opinion. Don't try to impose it on
me.

--
Regards,
Tom Ogilvy



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Copy Worksheet to another workbook, programatically?

Gentlemen! Please!
Tom's code worked swimmingly except I had to change:

sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)


to

Set shTo = ActiveSheet
shFrom.Copy After:=bkTo.ActiveSheet
'shFrom.Copy After:=bkTo.Worksheets.Count
Set shTo = ActiveSheet
shTo.Name = strNewSheetName

where the commented line is the application specific version of his original
line. The original version I generated Error 1004 (I forget which exact flavor,
application derived, something like that).
(One needs the repetition of: "Set shTo = ActiveSheet" or it renames the sheet
that the inserted one is copied after, rather than the new one.)

Thank you again so much, this group is a life-saver!
-plh

In article , Tom Ogilvy says...

Given Bob's valuable information:

' assume book1.xls is open, book2.xls may be open or not
' assume not default books, but books with these names,
' previously saved

Sub Tester1()
Dim bOpen
Dim bk1 As Workbook
Dim bk2 As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Application.ScreenUpdating = False
Set bk1 = Workbooks("Book1.xls")
Set sh1 = bk1.Worksheets("Sheet1")
On Error Resume Next
Set bk2 = Workbooks("Book2.xls")
bOpen = True
On Error GoTo 0
If bk2 Is Nothing Then
Set bk2 = Workbooks.Open("C:\My Folder\Book2.xls")
End If
sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)
Set sh2 = bk2.Worksheets(bk2.Worksheets.Count)
' or set sh2 = Activesheet
sh2.Name = "ABCD"
If Not bOpen Then
bk2.Close SaveChanges:=True
Else
bk2.Save
End If
Application.ScreenUpdating = True
End Sub

Code untested and may contain typos.
--
Regards,
Tom Ogilvy



I keep hitting "Esc", but I'm still here!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Copy Worksheet to another workbook, programatically?

Aach! Don't tell me! I just noticed! :-{0
-plh

In article , plh says...

Gentlemen! Please!
Tom's code worked swimmingly except I had to change:

sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)


to

Set shTo = ActiveSheet
shFrom.Copy After:=bkTo.ActiveSheet
'shFrom.Copy After:=bkTo.Worksheets.Count
Set shTo = ActiveSheet
shTo.Name = strNewSheetName

where the commented line is the application specific version of his original
line. The original version I generated Error 1004 (I forget which exact flavor,
application derived, something like that).
(One needs the repetition of: "Set shTo = ActiveSheet" or it renames the sheet
that the inserted one is copied after, rather than the new one.)

Thank you again so much, this group is a life-saver!
-plh

In article , Tom Ogilvy says...

Given Bob's valuable information:

' assume book1.xls is open, book2.xls may be open or not
' assume not default books, but books with these names,
' previously saved

Sub Tester1()
Dim bOpen
Dim bk1 As Workbook
Dim bk2 As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Application.ScreenUpdating = False
Set bk1 = Workbooks("Book1.xls")
Set sh1 = bk1.Worksheets("Sheet1")
On Error Resume Next
Set bk2 = Workbooks("Book2.xls")
bOpen = True
On Error GoTo 0
If bk2 Is Nothing Then
Set bk2 = Workbooks.Open("C:\My Folder\Book2.xls")
End If
sh1.Copy After:=bk2.Worksheets(bk2.Worksheets.Count)
Set sh2 = bk2.Worksheets(bk2.Worksheets.Count)
' or set sh2 = Activesheet
sh2.Name = "ABCD"
If Not bOpen Then
bk2.Close SaveChanges:=True
Else
bk2.Save
End If
Application.ScreenUpdating = True
End Sub

Code untested and may contain typos.
--
Regards,
Tom Ogilvy



I keep hitting "Esc", but I'm still here!


I keep hitting "Esc", but I'm still here!

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
Copy worksheet from one workbook to a master workbook mvannatta Excel Worksheet Functions 3 April 15th 09 08:32 PM
Copy Worksheet from one Workbook to another Workbook Autumn Dreams Excel Discussion (Misc queries) 5 September 12th 08 10:55 PM
How can I programatically change photos in one worksheet Steve Excel Discussion (Misc queries) 10 December 2nd 06 01:07 AM
Copy Data from Workbook into specific Worksheet in other Workbook? kingdt Excel Discussion (Misc queries) 1 March 16th 06 06:55 PM
copy worksheet from closed workbook to active workbook using vba mango Excel Worksheet Functions 6 December 9th 04 07:55 AM


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