+ Reply to Thread
Results 1 to 5 of 5

Macro to copy worksheet into a new workbook

  1. #1
    Registered User
    Join Date
    12-06-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Macro to copy worksheet into a new workbook

    Hi

    I've seen versions of this macro on the internet, but due to my poor VBA skills I'm unable to tailor it to my needs.

    What I need to do is:

    Have a macro linked to the save function of a excel workbook that copies a named "sourceworkbook" worksheet into a workbook with a specific name. I would like this macro to keep the name of the target wookbook the same, so for example "Targetexport.xlsx" and then overwrite this workbook everytime the macro is triggered.

    Will this be possible?

    Please forgive me if I've not explained very well.

    Thanks,

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Macro to copy worksheet into a new workbook

    Based upon the explanation you give, the first answer would be yes.
    I understand that tailoring macros is not and easy job, especially if they're not your own.
    You could record a macro and recreate the steps YOU want make.
    After you stop the macro recorder you can open the VBA editor and see what it has been created.
    Makes the macro easier to understand and then start practicing on writing and editing VBA code, it's the best way to learn, doing is learning.

    We all learned that way, if you look at all vba tutorials they start in the same manner. record a macro and look at the created code.
    If you have that code, then attach the file (with the code, non-private data please) and we can help you tailor, will make understanding VBA easier and then you won't have to depend on others
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    12-06-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    14

    Re: Macro to copy worksheet into a new workbook

    Thanks for your reply, Keebellah.

    Taking heed of your advice I recorded the macro and changed a few things that seemed obviously not specific enough to work in the context I need it to. I've now run it and made a lot of progress,

    so thanks a lot for your advice it really helped.

    I may post some questions re. the code I come up with eventually, but in the meantime I've got a lot to go on and hone.

    Thanks,

    EDIT - It seems like I've hit a snag, it's very nearly there - WorksheetA from WorkbookA gets copied to WorkbookB when WorkbookA is saved, woo hoo!

    However, when the code is run again I get worksheetA and worksheetA(2)

    how do I get the code overwritten so there is only ever a WorksheetA?

    here's the code:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Workbooks.Open Filename:="P:\B.xlsx"
    Windows("A.xlsm").Activate
    Sheets("worksheetA").Select
    Sheets("worksheetA").Copy Before:=Workbooks("B.xlsx").Sheets(1)
    ActiveWorkbook.Close savechanges:=True
    End Sub
    Last edited by Ben Bennett; 03-18-2019 at 01:01 PM.

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Macro to copy worksheet into a new workbook

    Hi Ben, use code tags when posting code.

    Your example is good but since you do not overwrite the file it will have a worksshtA already there so Excel automatically adds the (20 for the new sheet.
    This is the (my) solution

    Bysetting display alerts to fals it will overwrite the existing file

    Please Login or Register  to view this content.
    Give it a run. you don't neet to select and all that just this

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,905

    Re: Macro to copy worksheet into a new workbook

    Do keep in mind that this worksheet is saved every time your file is saved (so when you close the file and say yes when prompted to save.
    Caveat: hard-coding the path like you do P:\ means that anyone using this file must have access to a drive letter P:

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to copy column A from every worksheet in a workbook
    By naomip in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-29-2017, 11:02 AM
  2. I need to copy a macro from one worksheet to another within the same workbook
    By excelnewbe14 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2016, 03:59 PM
  3. [SOLVED] Need a Macro to Copy Worksheet template into the same workbook
    By justinprime in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-11-2012, 02:21 PM
  4. Macro: Copy from one workbook worksheet to a different workbook worksheet
    By Virgil Beach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2012, 01:13 AM
  5. Macro to copy from one worksheet to another in different workbook
    By Althas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2012, 02:42 AM
  6. Macro to copy specified worksheet to another workbook
    By AC1982 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-08-2010, 10:34 PM
  7. macro copy worksheet to new workbook and name it with cells value
    By jarssonn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2010, 07:59 AM

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