Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Copy Worksheet to a new Workbook creating if it doesn't exist and add more Worksheets if it does exist

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Copy Worksheet to a new Workbook creating if it doesn't exist and

Many thanks that worked perfectly, could i trouble you for a small
revision?

The worksheet names and workbook names will be variable as i change
them from data from the worksheet cells.

Is it possible to instead of "Destination.xls" make it the contents of
cells A1 and A2?

Is it possible instead of it being "sheetname" to be whatever the
worksheet name is?

Learning something new everyday.

Many Thanks


Peter

Tom Ogilvy wrote:

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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Copy Worksheet to a new Workbook creating if it doesn't exist and

Dim bk as Workbook, sh as worksheet
Dim sbk as String, ssh as String

sbk = Range("A1").Value & Range("A2").Value
ssh = ????
on Error resume next
set bk = workbooks(sBk)
if not bk is nothing then
set sh = bk.worksheets(ssh)
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\" & sb1
end if
On Error goto 0
thisworkbook.Worksheets(ssh).copy _
After:=bk.sheets(bk.sheets.count)
bk.Save

--
Regards,
Tom Ogilvy
wrote in message
oups.com...
Many thanks that worked perfectly, could i trouble you for a small
revision?

The worksheet names and workbook names will be variable as i change
them from data from the worksheet cells.

Is it possible to instead of "Destination.xls" make it the contents of
cells A1 and A2?

Is it possible instead of it being "sheetname" to be whatever the
worksheet name is?

Learning something new everyday.

Many Thanks


Peter

Tom Ogilvy wrote:

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





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Copy Worksheet to a new Workbook creating if it doesn't exist and

Thanks for the help,

I think i was right to change sb1 to sbk? if not it might explain my
problem!!

When the worksheet is copied to the new workbook, i lose some of the
data in cells with a certain formula (i'll work on changing that),
however it might be easier if the worksheet is just created in the new
worksheet as values, formats, column widths and that should remove the
problem.

Thanks again for all your help.

Peter

Tom Ogilvy wrote:

Dim bk as Workbook, sh as worksheet
Dim sbk as String, ssh as String

sbk = Range("A1").Value & Range("A2").Value
ssh = ????
on Error resume next
set bk = workbooks(sBk)
if not bk is nothing then
set sh = bk.worksheets(ssh)
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\" & sb1
end if
On Error goto 0
thisworkbook.Worksheets(ssh).copy _
After:=bk.sheets(bk.sheets.count)
bk.Save

--
Regards,
Tom Ogilvy
wrote in message
oups.com...
Many thanks that worked perfectly, could i trouble you for a small
revision?

The worksheet names and workbook names will be variable as i change
them from data from the worksheet cells.

Is it possible to instead of "Destination.xls" make it the contents of
cells A1 and A2?

Is it possible instead of it being "sheetname" to be whatever the
worksheet name is?

Learning something new everyday.

Many Thanks


Peter

Tom Ogilvy wrote:

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




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
does a value exist in another workbook chris 123456 Excel Worksheet Functions 4 February 19th 09 04:58 PM
Does worksheet exist BB Ivan Excel Discussion (Misc queries) 1 January 29th 09 10:04 PM
Does a worksheet exist? Alan Excel Programming 1 May 3rd 06 11:04 PM
Command Line. How to tell to XL : If the xls file exist : Open it, if it does not exist : Create it. Tintin92 Excel Programming 3 March 11th 06 06:45 PM
Does worksheet exist Robert S Excel Programming 3 May 18th 04 07:59 PM


All times are GMT +1. The time now is 10:50 PM.

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"