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!
|