+ Reply to Thread
Results 1 to 5 of 5

Problem with vb code when copying and pasting

  1. #1
    Registered User
    Join Date
    07-04-2012
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    2

    Problem with vb code when copying and pasting

    Hi,

    I have written the code below to copy a range from a saved workbook and to paste it into the active workbook that the project is saved in:

    Please Login or Register  to view this content.
    It works perfectly but I need to select another range from the same saved workbook so I re-wrote the code as follows:

    Please Login or Register  to view this content.
    When I run this code I get the error message:

    Run-time error '-2147221080 (80040198)':
    Method 'sheets' of object '_workbook' failed

    The line "Wb1.Sheets(1).Range("L1:L10").Copy' is highlighted in yellow when I go to de-bug.

    Can anyone tell me where I am going wrong please?

    Thanks very much in advance

    Michael
    Last edited by Richard Buttrey; 07-04-2012 at 03:44 PM. Reason: code tags

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Problem with vb code when copying and pasting

    Hi,


    Since you're new here you'll see that on this occasion I've edited your post and put code tags around your code. This is one of the forum rules. Please remember on other occasions.

    The line is failing because you've already closed Wb1.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    07-04-2012
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Problem with vb code when copying and pasting

    Thanks and sorry for not posting correctly.

    I'm really new to this so can you let me know how I can fix this?

    Thanks so much

    Michael

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Problem with vb code when copying and pasting

    Hi Michael and welcome to the forum.

    You need to learn to debug your code by putting in a breakpoint and stepping through the code a line at a time. Here is a great site for that:
    http://www.cpearson.com/excel/Debug.htm

    Then in looking at your code you close the workbook at the Wb1.Close line so when you get to the next Wb1.Sheets(1) it can't access a closed workbook.
    You will need to close the workbook after you're done with all the copy and paste steps.
    Read http://www.ozgrid.com/VBA/ExtractFromClosedWorkbook.htm that explains some of what I'm saying.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Problem with vb code when copying and pasting

    I have already mentioned the fix in my last comment. Implying you should close Wb1 after the paste.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1