+ Reply to Thread
Results 1 to 10 of 10

copying worksheet to same workbook and renaming it

  1. #1
    Registered User
    Join Date
    03-09-2009
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    7

    copying worksheet to same workbook and renaming it

    hi...
    i would like to use a macro which copies a worksheet from a workbook to the same workbook and paste it at the end and then renaming it... everything using a macro...
    appreciate the help...

  2. #2
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: copying worksheet to same workbook and renaming it

    Please Login or Register  to view this content.
    HTH

    NB, will crash if "Brian" already exists

    Please Login or Register  to view this content.
    is more robust - will simply add (x) after sheet name if duplicated
    Last edited by Cheeky Charlie; 03-10-2009 at 02:59 PM. Reason: duplicate sheet

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copying worksheet to same workbook and renaming it

    Go with:

    Please Login or Register  to view this content.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  4. #4
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: copying worksheet to same workbook and renaming it

    Quote Originally Posted by davegugg View Post
    Go with:

    Please Login or Register  to view this content.
    Don't. There is no need to activate the sheets.

    CC

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988

    Re: copying worksheet to same workbook and renaming it

    georgemathews or is it macro_user,


    Your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us help you!

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  6. #6
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copying worksheet to same workbook and renaming it

    I see that when you create the copy, vba automatically activates the copy sheet, but you have to have the sheet you are intending to copy already activated, right? So our code is essentially the same (except my Sheets(y).Activate), right Cheecky?

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: copying worksheet to same workbook and renaming it

    Well, no, there's no need to activate the sheet to copy it, perhaps you could try it:
    Please Login or Register  to view this content.
    with a different sheet activated - works just the same

    The OP asked for the sheet to be placed at the end (so my code: (a) finds the number of sheets and (b) copies the sheet after the last one)

    There's no need to activate the copy to rename it

    Functionally similar, but these details make a big difference in bigger projects - and piecing together these little bits is how most people on the forum will be learning

    CC

  8. #8
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copying worksheet to same workbook and renaming it

    I see what you mean about copying a sheet other than the active one. I am curious though - when you copy a sheet to the end, it seems that copy is automatically activated. Is that true? If so, does having a redundant Sheets("Copy").Activate make any difference? Does it slow down the code?

    Thanks, I'm trying to learn just like everyone else!

  9. #9
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: copying worksheet to same workbook and renaming it

    Yes, you're absolutely right, it is automatically activated.

    Yes, right again, it does slow the code - like I said it's not a big individual difference but it's the kind of thing which bloats a bigger project.

    Also, using things like "sheet.activate" encourages "lazy programming", like referring to ranges without addressing them to sheet level. So, if you make a macro to exist and run on a single sheet, it's probably fine just to address range(etc.etc.) but as soon as you start copying/moving/deleting sheets with your VBA the references which simply refer to range(etc) will become unreliable where sheets(sheet).range(etc.) will continue to work. Make sense?

    All these points are much more relevant to larger projects, but like I've said, it's by assembling the little bits that we make bigger projects so the building blocks should be as good as possible.

    And in terms of knowledge - in all fairness we're about a step away from each other on the learning curve with this.

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copying worksheet to same workbook and renaming it

    Thanks, that makes sense

+ 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