Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Select all sheets and copy

I need to create a copy of all sheets but I do not always know the names of
the sheets I'm copying. Here is the line of code I'm using today but I just
found out that one or more sheets may not exists always.

Sheets(Array("Bank " & BankNum & " Analyzed", "Pivot Table", "Bank " &
BankNum & " Delete Codes")).Copy

Is there a way to eliminate the names and just copy all sheets?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Select all sheets and copy

Dim bYes as Boolean, v as Variant
Dim i as Long, sh as Worksheet, sh1 as Worksheet
set sh1 = Activesheet
v = Array("Bank " & BankNum & " Analyzed", _
"Pivot Table", "Bank " & BankNum & " Delete Codes")
bYes = True
for i = lbound(v) to ubound(v)
set sh = Nothing
on error Resume Next
set sh = sheets(v(i))
on error goto 0
if not sh is nothing then
sh.Select Replace:=bYes
bYes = False
end if
Next
activeWindow.Selectedsheets.copy
sh1.Select

--
Regards,
Tom Ogilvy


"hshayh0rn" wrote:

I need to create a copy of all sheets but I do not always know the names of
the sheets I'm copying. Here is the line of code I'm using today but I just
found out that one or more sheets may not exists always.

Sheets(Array("Bank " & BankNum & " Analyzed", "Pivot Table", "Bank " &
BankNum & " Delete Codes")).Copy

Is there a way to eliminate the names and just copy all sheets?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Select all sheets and copy



For i = 1 To Worksheets.Count
Sheets(i).Copy Befo=Workbooks("Book2").Sheets(2)
Next wks

or

For each wks in worksheets
wks.copy Befo=Workbooks("Book2").Sheets(2)
Next wks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Select all sheets and copy

Thanks Brian. How would I use your statement if I wanted all of the sheets to
be copied to a new workbook?

"Brian Taylor" wrote:



For i = 1 To Worksheets.Count
Sheets(i).Copy Befo=Workbooks("Book2").Sheets(2)
Next wks

or

For each wks in worksheets
wks.copy Befo=Workbooks("Book2").Sheets(2)
Next wks


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Select all sheets and copy

If you want all the sheets copied

Sheets.Copy

--
Regards,
Tom Ogilvy


"hshayh0rn" wrote:

Thanks Brian. How would I use your statement if I wanted all of the sheets to
be copied to a new workbook?

"Brian Taylor" wrote:



For i = 1 To Worksheets.Count
Sheets(i).Copy Befo=Workbooks("Book2").Sheets(2)
Next wks

or

For each wks in worksheets
wks.copy Befo=Workbooks("Book2").Sheets(2)
Next wks




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Select all sheets and copy

One more (if you really meant copy all the sheets):

Sheets.copy



hshayh0rn wrote:

I need to create a copy of all sheets but I do not always know the names of
the sheets I'm copying. Here is the line of code I'm using today but I just
found out that one or more sheets may not exists always.

Sheets(Array("Bank " & BankNum & " Analyzed", "Pivot Table", "Bank " &
BankNum & " Delete Codes")).Copy

Is there a way to eliminate the names and just copy all sheets?


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Select all sheets and copy

Dave's solution is much more elegant than my long winded solution.

If you are trying to copy all sheets into a new workbook, then I think
Don's earlier question is valid. Why not do a save as?

Workbooks("MyWorkbook").SaveAs "C:\Temp\MySpecialWorkbook.xls"

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 74
Default Select all sheets and copy

sheets.copy worked great. I don't want to simply save it as another name
because I need to remove all of the code involved before it is save and I
thought just taking the sheets and copying them to a new workbook solved for
that the cleaniest.

"Dave Peterson" wrote:

One more (if you really meant copy all the sheets):

Sheets.copy



hshayh0rn wrote:

I need to create a copy of all sheets but I do not always know the names of
the sheets I'm copying. Here is the line of code I'm using today but I just
found out that one or more sheets may not exists always.

Sheets(Array("Bank " & BankNum & " Analyzed", "Pivot Table", "Bank " &
BankNum & " Delete Codes")).Copy

Is there a way to eliminate the names and just copy all sheets?


--

Dave Peterson

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
copy cell info to other sheets, other sheets dont contain all row. Ja Excel Worksheet Functions 1 November 1st 09 12:53 AM
Smart use of .Activate; .Select; .Copy with Sheets(1) va Worksheet Dennis Excel Discussion (Misc queries) 5 July 24th 05 01:05 AM
in VBA Sheets("mysheet").Copy Befo=Sheets(1) how do i get a reference to the newly created copy of this sheet? Daniel Excel Worksheet Functions 1 July 6th 05 09:57 PM
Copy select sheets to another Workbook Randy[_11_] Excel Programming 0 January 14th 04 05:06 PM
select a1 on all sheets Piers Clinton-Tarestad Excel Programming 1 October 1st 03 08:34 PM


All times are GMT +1. The time now is 09:44 AM.

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"