Consulting

Results 1 to 11 of 11

Thread: Move a copy of worksheets

  1. #1

    Move a copy of worksheets

    I am looking for a piece of code to copy an array of worksheets to a new workbook.

    Thanks.

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi primfran,

    Try:
    Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Copy
    Just enclose the sheet names in the Array(), and using .Copy by itself on the Sheets will create a new workbook for them.

    Matt

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this.

    Option Explicit
    
    Sub CopySheets()
    Dim WkbOld          As Workbook
    Dim WkbNew          As Workbook
    Dim SheetArray()    As String
    Dim i               As Long
    Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    ReDim SheetArray(1 To 3)
        SheetArray(1) = "Sheet1"
        SheetArray(2) = "Sheet2"
        SheetArray(3) = "Sheet3"
    Set WkbOld = ActiveWorkbook
        Set WkbNew = Workbooks.Add
    For i = 1 To WkbNew.Sheets.Count - 1
            WkbNew.Sheets(2).Delete
        Next
        WkbNew.Sheets(1).Name = "DelMe"
    For i = 1 To UBound(SheetArray)
            WkbOld.Sheets(SheetArray(i)).Copy After:= _
            WkbNew.Sheets(WkbNew.Sheets.Count)
        Next i
    WkbNew.Sheets("DelMe").Delete
    Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub

  4. #4
    Perfect. Thanks.

    How do you select a range of columns over an array of worksheets if the starting column changes?

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    How do you determine what the starting column is?

  6. #6
    It is entered into an input box, then I use the find function to locate the column with that cell in it.

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Can you post all relevant code? If we can see the whole picture it will be much easier to ascertain a valid and working solution for you.

  8. #8
    Today = Format(InputBox("Enter the report date", "Enter Date"), "mm/dd/yyyy")
    Cells.Find(Today).Activate
    Col = ActiveCell.Column
    I then want to hide all columns after the column with the cell that has the value in it.

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Try this.

    Sheets("Sheet1").Range(Cells(1, Col), _
    Cells(65536, 256)).EntireColumn.Hidden = True

  10. #10
    That worked. Thanks.

  11. #11
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •