Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Ignorance 1

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

In Excel, I want to programmatically group and move all the sheets in a workbook to a new book; however, my ability to comprehend arrays is minimal in VB.

This is what the code would look like (using the macro recorder).

Code:
Sheets(Array("62321MAIL", "Template")).Select
    Sheets("Template").Activate
    Sheets(Array("62321MAIL", "Template")).Copy

The number of sheets will vary, and I don't know how to concatenate an array.

Any help would be greatly appreciated.

Thanks,



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 


Hi,

Rather than that, just copy each sheet to the new workbook. Procedure CopySheets is in a module in the workbook containing the sheets you want to move.
Code:
Sub CopySheets()
    Dim wbNew As Workbook, ws As Worksheet
    
    Set wbNew = Workbooks.Add
    
    For Each ws In ThisWorkbook.Worksheets
        ws.Copy Before:=wbNew.Sheets(1)
    Next
End Sub

Skip,

[glasses] [red]Be Advised![/red] A chicken, who would drag a wagon across the road for 2 cents, is…
POULTRY in motion to PULLET for a PALTRY amount! [tongue]
 
Skip:

I had tried something similar, but it kept copying them to new workbooks.

Thanks a star.

Ron



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top