+ Reply to Thread
Results 1 to 5 of 5

Is there better code to copy and add sheets to a workbook?

  1. #1
    Registered User
    Join Date
    12-15-2011
    Location
    Charleston, SC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Is there better code to copy and add sheets to a workbook?

    I'm trying to learn about the code generated from recorded macros and what code can be deleted. Below is code taken from a larger macro and I'm curious if there is a better process.

    Let me explain in a little more detail about this one section. I'm exporting an inventory report from a database into Excel and the workbook has one tab with the name of "Inv Report". I need this sheet to be copied to a new tab. These tabs will be named "Compare" and "Before". Next is to add a new tab named "After" which will remain blank for now. The tabs will need to be ordered... Compare, After, Before.

    Now the code works just fine the way it is and I can live with it. However, I'm in search of knowledge and to learn better ways of making Excel work.

    Please Login or Register  to view this content.
    Last edited by BBoydAnchor; 12-29-2011 at 08:14 AM.

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Is there better code to copy and add sheets to a workbook?

    Save some typing using With/End With. Also, add a sheet...
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-15-2011
    Location
    Charleston, SC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Is there better code to copy and add sheets to a workbook?

    Thanks for your input, but I'm not clear on your response. As for save some typing, this was a recorded macro and so I did not type anything. I deleted code from this part of the macro that I determined was not needed.

    The code you provided only appears to address part of my question. I did add it to my code where I thought it should go...

    Please Login or Register  to view this content.
    However, I received the following error message.

    Run-time error '1004:
    Method 'Add" of Object 'Sheets" failed

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Is there better code to copy and add sheets to a workbook?

    Try this
    Please Login or Register  to view this content.
    Last edited by royUK; 12-29-2011 at 04:33 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    12-15-2011
    Location
    Charleston, SC
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Is there better code to copy and add sheets to a workbook?

    Thank you RoyUK! The code works perfectly, but most importantly is that I'm learning.

    Have a wonderful day!

+ 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