Consulting

Results 1 to 3 of 3

Thread: Getting and Extra Worksheet

  1. #1

    Getting and Extra Worksheet

    I have this code...

    [VBA]Set oWB = Workbooks.Open(Me.txt_HCSA_oldFilename.Value)
    For Each wSht In oWB.Worksheets
    If Range("A1").Value <> "" Then
    oWB.Worksheets.Copy 'after:=oThis.Worksheets(oThis.Worksheets.Count)
    oThis.Sheets(wSht).Name = "Sheet1"
    oWB.Close SaveChanges:=False

    End If
    Next wSht[/VBA]

    And I'm really trying to copy a worksheet from 1 excel file to the open excel file and rename it to "sheet1".

    However when I run this it does the copy just fine but it also creates a BLANK worksheet with the name "Sheet1" in the open excel file. and I get a error when I try to rename.

    Why, What's wrong?

    Thanks,
    Last edited by robsimons; 11-08-2007 at 11:50 AM.

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    This line:
    [VBA]oWB.Worksheets.Copy[/VBA]
    copies all the worksheets to a new workbook for each worksheet in the existing book, since you didn't specify a single worksheet to copy or a destination.
    This line:
    [VBA]oThis.Sheets(wSht).Name = "Sheet1"[/VBA]
    will fail because wSht is a worksheet object, not an index number or the name of a sheet.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    thanks this did help a little.

Posting Permissions

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