Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Copy worksheet from one workbook to a master workbook | Excel Worksheet Functions | |||
Copy Worksheet from one Workbook to another Workbook | Excel Discussion (Misc queries) | |||
How can I programatically change photos in one worksheet | Excel Discussion (Misc queries) | |||
Copy Data from Workbook into specific Worksheet in other Workbook? | Excel Discussion (Misc queries) | |||
copy worksheet from closed workbook to active workbook using vba | Excel Worksheet Functions |