+ Reply to Thread
Results 1 to 18 of 18

VBA to create workbook from sheet name, check if file exists

  1. #1
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    VBA to create workbook from sheet name, check if file exists

    Hi

    I am trying to create a new workbook for each sheet in my workbook. So far I have this:

    Please Login or Register  to view this content.
    what i am trying to do next is to check if a workbook with the sheet name already exists. If it does, I want to copy the data to the bottom of the existing workbook. If it doesn't, I want to create a new workbook as above.

    Can someone please help me solve this?

    Thanks

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: VBA to create workbook from sheet name, check if file exists

    You can check existance of a file with the Dir-command.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    Hi bakerman2,

    thanks for your reply, but can you please elaborate on how to do this?

    Thanks

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    Please Login or Register  to view this content.
    you are making a brand new folder which is date and time stamped every time you run this...there would be no existing files in that folder?
    are you referring to subfolders?
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  5. #5
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    I don't actually need a brand new folder each time. I only want to reference the folder, check if there is a workbook with the name of the sheet. If there is, I want to open it and add the data. If there isn't, I want to create a new one as above.

    Thanks

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists



    anyway
    Please Login or Register  to view this content.
    http://www.rondebruin.nl/win/s9/win003.htm

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    i am confused about your pathing but in your case you would replace
    FilePath = "C:\Users\Ron\test\book1.xlsm"
    however your path works then
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    thanks but I can't get this. If all my files were in C:\Users\Ron\test\

    then I am trying to use something like this to check (sourcewb is active workbook)"

    C:\Users\Ron\test\" & Sourcewb & sh.Name & ".*"

    but it is giving me an error?

  9. #9
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    I tried to loop but that also doesn't work:

    Please Login or Register  to view this content.
    Is there something that I am missing?

  10. #10
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    re arranged code to work with sheet name

    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    in the attached example just put a file named testfile in there and you will see it work
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    thank you so much. I am trying to create the file if it doesn't exist, however I get an error where I try to save it:

    Please Login or Register  to view this content.
    Is there something I have done incorrectly?

  13. #13
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    first off..you should probably ident properly to find out what code goes with what

    some minor tweaks to the code below

    Please Login or Register  to view this content.
    Last edited by humdingaling; 07-08-2016 at 12:31 AM. Reason: extra comments

  14. #14
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    Thank you, that works grreat.

    Lastly, if the workbook exists, I need to open it and copy the contents to the bottom of it. Is there a way I can do this?

  15. #15
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    yes but i have no idea what to copy from original sheet
    you cant copy the whole sheet and paste into another whole sheet...ie A1 to XFD1048576 it just wont fit

    you need to be specific about what you are copying and where you are copying it to

  16. #16
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    thank you.

    I would like to copy from A2 to column D (then down to last row) and then add this to the bottom of the data in the existing worksheet (first available row). Does that make sense? I can post an example if that is easier.

  17. #17
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: VBA to create workbook from sheet name, check if file exists

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: VBA to create workbook from sheet name, check if file exists

    works perfectly, thank you so much

+ 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. [SOLVED] VBA Check if Sheet Exists, if it does, run code. Test File Attached
    By unit285 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-14-2016, 01:23 PM
  2. Check if Sheet Name exists Then Error or continue Create
    By alimsab in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2015, 02:55 PM
  3. [SOLVED] Check if folder exists, Create if Not
    By szpt9m in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2014, 04:03 AM
  4. [SOLVED] check if sheet exists, if not then create
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-06-2013, 06:12 AM
  5. Check if sheet exists in another workbook
    By arlu1201 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-12-2013, 02:49 PM
  6. Check if sheet from list exists in workbook
    By arlu1201 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-25-2011, 02:13 PM
  7. Check if sheet exists in a closed workbook
    By FrigidDigit in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2005, 02:05 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