View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default Copy Worksheet to a new Workbook creating if it doesn't exist and

assume you mean if the workbook is open in excel

Dim bk as Workbook, sh as worksheet
on Error resume next
set bk = workbooks("Destination.xls")
if not bk is nothing then
set sh = bk.worksheets("SheetName")
if not sh is nothing then
application.Displayalerts = False
sh.Delete
application.DisplayAlerts = True
end if
else
set bk = workbooks.Add()
bk.SaveAs "C:\Myfolder\Destination.xls"
end if
On Error goto 0
thisworkbook.Worksheets("SheetName").copy _
After:=bk.sheets(bk.sheets.count)
bk.Save

--
Regards,
Tom Ogilvy



" wrote:

Help Please with VBA

I would like to copy a worksheet from an active workbook creating a new
workbook if it does not exist. If it exists just to add the worksheet
to it. Also need it to overwrite a sheet with the same name.

I am using code to save the sheet by cell references and can get as far
as saving to a new workbook, but cannot get to add a sheet, only to
overwrite the file.

Sounded so simply when i started!! All help greatly appreciated.

Regards and Thanks


Peter