Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Moving sheets to another workbook

Hi!

I have a source workbook which creates 25 to 30 worksheets, each one's name
ends with "P1"

I want to move these sheets to an existing destination workbook, through a
vba sub residing in the source workbook

BUT :
if one or the source worksheet exists in the destination workbook, it
should be replaced by the new one


I've tried to code that, but Excel generates an error and closes.


Any idea for coding that ?

Thanks by advance,
Herve+
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 486
Default Moving sheets to another workbook

try this...

Sub Test()
Dim wbkSource As Workbook
Dim wbkDestination As Workbook
Dim wksSource As Worksheet

On Error GoTo ErrorHandler
Application.DisplayAlerts = False
'Set your source
Set wbkSource = ThisWorkbook
Set wksSource = wbkSource.Sheets("Sheet1")

'Set your destination
On Error Resume Next
Set wbkDestination = Workbooks("ThatBook.xls")
On Error GoTo ErrorHandler
If wbkDestination Is Nothing Then
Set wbkDestination = Workbooks.Open("C:\Thatbook.xls")
End If

'You now have all of your souce and destination objects
If SheetExists(wksSource.Name, wbkDestination) Then
wbkDestination.Sheets(wksSource.Name).Delete
End If
wksSource.Copy wbkDestination.Sheets(1)

ErrorHandler:
Application.DisplayAlerts = True
End Sub

Public Function SheetExists(SName As String, _
Optional ByVal Wb As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If Wb Is Nothing Then Set Wb = ThisWorkbook
SheetExists = CBool(Len(Wb.Sheets(SName).Name))
End Function
--
HTH...

Jim Thomlinson


"affordsol" wrote:

Hi!

I have a source workbook which creates 25 to 30 worksheets, each one's name
ends with "P1"

I want to move these sheets to an existing destination workbook, through a
vba sub residing in the source workbook

BUT :
if one or the source worksheet exists in the destination workbook, it
should be replaced by the new one


I've tried to code that, but Excel generates an error and closes.


Any idea for coding that ?

Thanks by advance,
Herve+

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
Moving Data between sheets in the same workbook and moving data between Workbooks. Alison Brown Excel Worksheet Functions 0 February 10th 09 01:03 AM
moving data between workbook sheets Soosieboo Excel Discussion (Misc queries) 3 September 23rd 08 10:45 PM
Moving Sheets within a workbook dan Excel Worksheet Functions 5 August 4th 06 10:58 PM
Moving Macros with Sheets to New Workbook Brad Excel Programming 2 January 18th 05 04:32 PM
Moving a variable number of Sheets to another Workbook Nelson M Excel Programming 2 January 16th 04 03:01 PM


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