Consulting

Results 1 to 4 of 4

Thread: Copy Worksheet

  1. #1

    Question Copy Worksheet

    Hello all
    I am trying to copy a sheet from another workbook into my current workbook using the following code:
    'Get the active workbook name
        ExistingExcelBook = ActiveWorkbook.FullName
     
        Workbooks.Open (NewExcelBook)
        Workbooks(NewExcelBook).Activate
        Workbooks(NewExcelBook).Sheets("Sheet1").Name = "Import"
        Workbooks(NewExcelBook).Worksheets("Import").Visible = True
        Workbooks(NewExcelBook).Sheets("Import").Select
        Workbooks(NewExcelBook).Sheets("Import").Activate
        Workbooks(NewExcelBook).Sheets("Import").Copy After:=Workbooks(ExistingExcelBook).Sheets("Sheet2")
    Unfortunately I get an error: 'Subscript out of range' when I run this and for the life of me I cannot work out why. In the past I have had this when iterating through sheets or collections.
    The code stops at Workbooks(NewExcelBook).Activate
    The thing is that the Workbooks.Open (NewExcelBook) code does open the workbook because it appears on the task bar below and I can look at it to see all of the data. The other thing is that there is a sheet1 so it is not like it does not exist!!
    Any ideas anyone?
    Any help is appreciated.
    Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You don't need to activate it, opening it makes it active.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Quote Originally Posted by xld
    You don't need to activate it, opening it makes it active.
    Even if I comment this out, it still stops with the same error on the next line down:
    Workbooks(NewExcelBook).Sheets("Sheet1").Name = "Import"
    I know that there is a Sheet1 so I cannot understand why I get the same message!!

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That would suggest to me that ExcelName contains the full path of the workbbok, but you refer to a workbook by its name, not its full name

    E.g. Mybook.xls, not C:\library\2008\MyBook.xls.

    The best way is to set a workbook object when opening, and use that

    [vba]

    'Get the active workbook name
    ExistingExcelBook = ActiveWorkbook.FullName

    Set wb = Workbooks.Open(NewExcelBook)
    With wb
    .Sheets("Sheet1").Name = "Import"
    .Worksheets("Import").Visible = True
    .Sheets("Import").Select
    .Sheets("Import").Activate
    .Sheets("Import").Copy After:=Workbooks(ExistingExcelBook).Sheets("Sheet2")
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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