+ Reply to Thread
Results 1 to 5 of 5

VBA Copy and Create File Using Sheet Name

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Copy and Create File Using Sheet Name

    Hi, I wonder whether someone may be able to help me please.

    I'm trying to put together a script which peforms the following:

    • Takes all but four sheets in a workbook,
    • Copies each sheet, and then
    • Pastes this into a new file, using the sheet name as the filename, and then
    • Automatically save each file to a specific folder

    The code below is what I've put together, but unfortunately I'm receiving a 'Run time error '91' with debug highlighting this line as the cause:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I just wondered whether someone could possibly look at this please and let me know where I'm going wrong, and provide some guidance on how I may be able to automatically save the file to a specific location, which is "Z:\Extracted Reports"

    Many thanks and kind regards

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,626

    Re: VBA Copy and Create File Using Sheet Name

    in the beginning
    Please Login or Register  to view this content.
    But then have a look on variable sh. You (OK, not in person, VBA did it :-) ) went whole loop:

    Please Login or Register  to view this content.
    in the loop it was easy - sh pointed to just one of worksheets.

    but after the loop?
    So neither sh.name nor sh.copy would be right calls.


    As it probably is the continuation of the other therad, my question from the previous one still applies:

    Why do you want to select an array of sheets.

    From the descriprion in present thread it seems that you want to have each sheet (except of four) in separate file. So again - no reason to select several sheets at once.

    If I am right in my understanding (each sheet (except of four) copied to separate file) try:

    Please Login or Register  to view this content.
    if you meant something else - try (I have not corrected indenting here, but please compare above and below code - can you notice the difference in easiness of reading?):

    Please Login or Register  to view this content.
    Last edited by Kaper; 01-28-2014 at 08:07 AM.

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Copy and Create File Using Sheet Name

    Hi @Kaper, thank you very much for taking the time to reply to my post, and for the comprehensive reply, I've found it to be very interesting.

    You can probably tell that I'm relatively new to VB and so I will, and obviously have made some basic errors. So in respect of setting an array, this was the only way in which I knew to select all the relevant sheets, but I'm more than happy to be told of a better method.

    In relation to what I'm trying to achieve:
    • Excluding the four sheets highlighted,
    • Copy each sheet independently, then
    • Paste into a new workbook, then
    • Save each file to the location "Z:\Extracted Files" with the sheet name being used as the filename

    I hope this helps, and once again, many thanks for your help.

    Kind Regards

  4. #4
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Copy and Create File Using Sheet Name

    Hi @Kaper, I hope you are well.

    I feel I need to offer you my sincere apologies, because when I replied to your post yesterday I clearly did so without reading your post properly.

    The solution you kindly provided (copy below) works perfectly. I just made some minor tweaks by adding a 'Save Directory' line of code, and it's exactly what I was after.

    Please Login or Register  to view this content.
    Once again my apologies and thanks

    All the best and kind regards

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,626

    Re: VBA Copy and Create File Using Sheet Name

    Glad it worked, and you finally got wheat you planned.
    Best Regards,

    Kaper

+ 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. Copy / Paste Values Only one sheet into another file from a multi sheet excel file
    By srisri201012 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-27-2013, 12:56 PM
  2. Copy Sheet / Create & Name New Sheet / Insert Before a Sheet / Paste Data
    By thinkspac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2012, 02:27 PM
  3. Filter, copy filtered data into another sheet, create a file of that sheet..
    By titushanke in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-17-2012, 02:26 PM
  4. Copy Sheet & Create New Monthly Sheet From Present Sheet
    By unley in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2010, 12:00 PM
  5. Need to create a button that can copy cells, create a new sheet and then paste there
    By torontoguy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-14-2009, 11:26 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