+ Reply to Thread
Results 1 to 12 of 12

Macro to copy sheet from closed work book and overwrite if sheet already exists

  1. #1
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Macro to copy sheet from closed work book and overwrite if sheet already exists

    I am currently using a macro to copy a sheet from a closed workbook in to my current workbook. However this copying is based on the sheet name. At present when I run the following code

    Please Login or Register  to view this content.
    It does exactly what I want. However if I specify to run the macro again with the same sheet name to be copied I get an error ""Cannot rename a sheet to the same name as another sheet" however I want to be able to overwrite the sheet with the data. How would I edit the macro code to be able to overwrite?

    Thanks for any help
    Last edited by munkee; 11-23-2009 at 09:16 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with overwriting

    Add some code to delete the sheet it is replacing before it is copied in.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Re: Help with overwriting

    Would someone be able to write that for me? im a complete macro noob and ive been editing verisions of other peoples so deleting i got no clue on how to write that

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with overwriting

    Whomever helped you construct the current macro should be able to adjust it to delete a sheet when needed.

  5. #5
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Re: Help with overwriting

    Found it on a website archive sadly

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with overwriting

    APOLOGIES...for the sarcastic post. I'd forgotten the link in the original post to the code being used.

    Good job Trevor. Just right.
    Last edited by JBeaucaire; 11-23-2009 at 12:28 AM.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

    Re: Help with overwriting

    Hi munkee,

    While it's always tricky to modify someone else's code, try this:

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

    Re: Help with overwriting

    Thanks JBeaucaire

  9. #9
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Re: Help with overwriting

    Thanks for the help trebor but I have found an issue. If there is not a sheet with the same name as newtabname i get an error on this line:

    If Len(Sheets(newtabname).Name) > 0 Then

    However if there is a sheet with the newtabname already present within the workbook everything over writes and runs perfect. I assume there is something simple missing.

    Current code:

    Please Login or Register  to view this content.
    Last edited by munkee; 11-23-2009 at 08:43 AM.

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

    Re: Help with overwriting

    Hi munkee,

    I may have a solution for you but for me to provide it you'll have to ask a moderator to change your thread title to something meaningful and have your code placed (wrapped) in code tags as your thread is currently in violation of forum rules one and three respectively (see here for further details).

    Regards,

    Robert

  11. #11
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Re: Macro to copy sheet from closed work book and overwrite if sheet already exists

    Should all be sorted according to forum rules now Trebor

  12. #12
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

    Re: Macro to copy sheet from closed work book and overwrite if sheet already exists

    Thanks munkee

    Let me know how this goes:

    Please Login or Register  to view this content.
    HTH

    Robert

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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