Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copy all worksheets to another workbook, excl. duplicate sheets already in other workbook


Hi all!

I must say that this forum has been helpful in leaps and bounds!

Anyways, I have code that cycles through all sheets in workbook1, and
then copies a specific range to the corresponding worksheets (with like
names) in workbook2. [PHASE 1]

Now my problem is that I need code that will cycle through all
worksheets in workbook1, and compare the names of them to those in
workbook2. If a worksheet (i.e. worksheet name) exists in the first
workbook, but not in the second workbook, then this specific sheet
(including all formulas, formats etc.) in its entirety must be copied to
the second workbook...[PHASE 2]

Bear in mind that an indefinite number of worksheets will be added over
time (into workbook1), of which the sheet names will be unknown (i.e. I
may not use specific sheet names in the macro (i.e worksheets
("ListA").Copy, etc. can not be used. ALSO, The amount of sheets
workbook1 is unknown.

Makes sense?
If it does, please help!!!
SOS
:)

Here's the code I've got for the first phase:

Sub Copy_Stuff ()

Dim TargetWB As Workbook
Dim SourceWB As Workbook
Dim TargetWS As Worksheet
Dim SourceWS As Worksheet
Dim SourceRange As Range

Set TargetWB = Workbooks.Open(ThisWorkbook.Path & "\Price Lists1.xlsm",
ignorereadonlyrecommended:=True)

Set SourceWB = Workbooks.Open(ThisWorkbook.Path & "\Price Lists2.xlsm",
ignorereadonlyrecommended:=True)


Workbooks("Price Lists2.xlsm").Activate
For Each SourceWS In Worksheets
Set SourceRange = SourceWS.Range("C13:X100")

Workbooks("Price Lists1.xlsm").Activate
For Each TargetWS In Worksheets
If TargetWS.Name = SourceWS.Name Then
SourceRange.Copy
TargetWS.Range("C13").PasteSpecial xlPasteAll

'PLACE NEW CODE HERE...??? after ELSE
statement...???

Exit For
End If
Next TargetWS
Next SourceWS

Set TargetWB = Nothing
Set SourceWB = Nothing
Set SourceRange = Nothing

End Sub

Any Idea of code I can use for phase 2 in the ['PLACE NEW CODE
HERE...??? after ELSE statement...???] area?

Any other suggestions are also welcome.

Thanx in advance...
ARbitOUR


--
ARbitOUR
------------------------------------------------------------------------
ARbitOUR's Profile: http://www.thecodecage.com/forumz/member.php?userid=254
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=100430

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Copy all worksheets to another workbook, excl. duplicate sheets already in other workbook

Use code like the following:

Dim WB1 As Workbook
Dim WB2 As Workbook
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim B As Boolean

Set WB1 = Workbooks("Book1.xls") '<< CHANGE
Set WB2 = Workbooks("Book2.xls") '<< CHANGE
On Error Resume Next
For Each WS1 In WB1.Worksheets
B = Not IsError(WB2.Worksheets(WS1.Name))
If B = True Then
With WB2.Worksheets
WS1.Copy after:=.Item(.Count)
End With
End If
Next WS1

This will copy all sheets in WB1 to the end of the worksheets in WB2,
creating a new worksheet.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Tue, 26 May 2009 15:58:39 +0100, ARbitOUR
wrote:


Hi all!

I must say that this forum has been helpful in leaps and bounds!

Anyways, I have code that cycles through all sheets in workbook1, and
then copies a specific range to the corresponding worksheets (with like
names) in workbook2. [PHASE 1]

Now my problem is that I need code that will cycle through all
worksheets in workbook1, and compare the names of them to those in
workbook2. If a worksheet (i.e. worksheet name) exists in the first
workbook, but not in the second workbook, then this specific sheet
(including all formulas, formats etc.) in its entirety must be copied to
the second workbook...[PHASE 2]

Bear in mind that an indefinite number of worksheets will be added over
time (into workbook1), of which the sheet names will be unknown (i.e. I
may not use specific sheet names in the macro (i.e worksheets
("ListA").Copy, etc. can not be used. ALSO, The amount of sheets
workbook1 is unknown.

Makes sense?
If it does, please help!!!
SOS
:)

Here's the code I've got for the first phase:

Sub Copy_Stuff ()

Dim TargetWB As Workbook
Dim SourceWB As Workbook
Dim TargetWS As Worksheet
Dim SourceWS As Worksheet
Dim SourceRange As Range

Set TargetWB = Workbooks.Open(ThisWorkbook.Path & "\Price Lists1.xlsm",
ignorereadonlyrecommended:=True)

Set SourceWB = Workbooks.Open(ThisWorkbook.Path & "\Price Lists2.xlsm",
ignorereadonlyrecommended:=True)


Workbooks("Price Lists2.xlsm").Activate
For Each SourceWS In Worksheets
Set SourceRange = SourceWS.Range("C13:X100")

Workbooks("Price Lists1.xlsm").Activate
For Each TargetWS In Worksheets
If TargetWS.Name = SourceWS.Name Then
SourceRange.Copy
TargetWS.Range("C13").PasteSpecial xlPasteAll

'PLACE NEW CODE HERE...??? after ELSE
statement...???

Exit For
End If
Next TargetWS
Next SourceWS

Set TargetWB = Nothing
Set SourceWB = Nothing
Set SourceRange = Nothing

End Sub

Any Idea of code I can use for phase 2 in the ['PLACE NEW CODE
HERE...??? after ELSE statement...???] area?

Any other suggestions are also welcome.

Thanx in advance...
ARbitOUR

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default [SOLVED]: Copy all worksheets to another workbook, excl. duplicate sheets already in other


Hi there. Thx for the code.

Unfortunately (with a bit of hindsight) I realized that adjusting my
code to allow the changes I requested would actually cause other
problems that would negate certain functions of other code I already
have in the applicable workbooks.


Thanx though for the interest!

ARbitOUR


--
ARbitOUR
------------------------------------------------------------------------
ARbitOUR's Profile: http://www.thecodecage.com/forumz/member.php?userid=254
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=100430

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
How do I duplicate value in multiple worksheets in same workbook? jwnorris Excel Discussion (Misc queries) 3 December 4th 05 08:01 PM
copy sheets in workbook to new workbook Ctech[_56_] Excel Programming 1 November 23rd 05 08:09 PM
Copy four worksheets from one workbook into a new workbook.e-mail Francis Brown Excel Programming 1 October 3rd 05 12:24 AM
how do i look through worksheets in a workbook to find duplicate Connie Excel Worksheet Functions 1 June 25th 05 05:10 PM
copy a workbook from other workbook with lot of sheets wit... Vai Excel Discussion (Misc queries) 1 January 3rd 05 10:27 PM


All times are GMT +1. The time now is 12:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"