+ Reply to Thread
Results 1 to 16 of 16

VBA to copy Worksheet to new Workbook

  1. #1
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    VBA to copy Worksheet to new Workbook

    I am using VBA to cycle through an array, and save each workbook with a new file name. What I am wanting to do is while I have the workbooks open, copy a worksheet from the open workbook to a new workbook. So for example, let's say my Array("Monday", "Tuesday", "Wednesday") and each workbook always contains two worksheets New and Old. When I have Monday open, I want to copy the worksheet "New" to a new workbook. I would need to keep that new workbook open tho, because I want to copy the same worksheet from Tuesday, and Wednesday to the "new" workbook as well. How would VBA accomplish this?
    Last edited by jo15765; 01-24-2012 at 04:49 PM.

  2. #2
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    To copy a sheet to a brand new workbook requires simply
    Please Login or Register  to view this content.
    you can then use
    Please Login or Register  to view this content.
    to get a reference to the newly created workbook. Thereafter

    Please Login or Register  to view this content.
    as an example.
    Good luck.

  3. #3
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    How would I create a new workbook to copy to? Is it
    Please Login or Register  to view this content.

  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: VBA to copy Worksheet to new Workbook

    Great tips from OnErrorGoto0, one small tweak I would suggest:

    Please Login or Register  to view this content.
    _________________
    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!)

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    @JB
    Depends where you want the sheet!

    @jo15765

    The first line of code creates a new workbook containing just the copied sheet. If you do not specify a destination, you will always get a new workbook.

  6. #6
    Valued Forum Contributor john55's Avatar
    Join Date
    10-23-2010
    Location
    Europe
    MS-Off Ver
    Excel for Microsoft 365
    Posts
    2,028

    Re: VBA to copy Worksheet to new Workbook

    sorry!..my mistake!
    Last edited by john55; 01-24-2012 at 11:40 AM.
    Regards, John55
    If you have issues with Code I've provided, I appreciate your feedback.
    In the event Code provided resolves your issue, please mark your Thread as SOLVED.
    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

    ...enjoy -funny parrots-

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

    Re: VBA to copy Worksheet to new Workbook

    Based on the OPs indication he is cycling through sheets Monday-Wednesday.... putting them into the new workbook in the same order would seem prudent, thus my suggestion. Without it, the sheets would end up in the new workbook Mon-Wed-Tues. That may not matter.


    It's also worth noting that if the sheets being copied all have the same name "NEW" then you will have trouble copying them into a new workbook without renaming something.

  8. #8
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    I take your point regarding the order. Either that or use before:=wb.Sheets(1) if you want the latest sheet first - I presumed that the names were fictitious.

  9. #9
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    If Copies to a new workbook
    Please Login or Register  to view this content.
    But I need to use
    Please Login or Register  to view this content.
    To copy subsequent sheets...I haven't defined a "WorkbookName" yet so how would it work?

  10. #10
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    Workbook name refers to the workbook you are copying from, not to. wb is the destination workbook.

  11. #11
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    With the workbook name do I need to include the extension? .xls?

  12. #12
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    I also need to sort the workbooks two ways...The Mon - Wed books need the worksheets copied to one workbook, and the Thurs Fri workbooks need the worksheets copied into a different workbook. I know I could break up the "Master" Procedure I am running, but I would rather that not occur. Can you provide a fix, maybe somehow name the Mon - Wed copy to wb1 and Thurs Fri copy to wb2 or something?

  13. #13
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    Yes, you should include the file extension (or store a reference to the workbook in a variable if your code opens it as well).

    I cannot tell you how to best adapt your code since you have not posted any, but yes you can use two workbook variables and simply copy the sheets from the first three books to one and the second two books to another.

    On a slight tangent, I note that you post your questions in several different forums (without cross-posting, happily) - is there a reason for this? It occurs to me that if you have several questions on parts of the same project, it might make life simpler if a few people on one or two forums became familiar with the general theme. (Though obviously there are no guarantees that this would happen!)

  14. #14
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    I'll play with the coding to try and set Mon - Wed copy to sort of a wb1 and Thu-Fri copy to a 2nd workbook or wb2

    I guess I should re-read the forum rules (I didn't know about cross posting) but that does makes sense about giving the full picture so that code could be adapted to better suit my needs.

  15. #15
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: VBA to copy Worksheet to new Workbook

    Apologies, I think you misunderstood my point. As far as I can see, you do not cross-post - which is posting the same question on several forums. You post different questions on different forums - I was simply intrigued as to the reason (though it is none of my business really so please do not feel obligated to answer!)

  16. #16
    Forum Contributor
    Join Date
    06-18-2010
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    546

    Re: VBA to copy Worksheet to new Workbook

    I do frequent a few different forums, and the reason is the firewall here at work is very finicky. Often times it shows this website as being blocked and not accessible. Or I get the message that this site could be potentially unsafe, if you feel that it should be added to the "Safe List" contact network admin. So when those types of errors occur, I move onto another site that the firewall will permit me to view.

+ 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